You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server-monitor.c 27KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <mosquitto.h>
  7. #include <time.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. static int debug = 0;
  12. #define MQTT_SERVER_IP "127.0.0.1"
  13. #define MQTT_SERVER_PORT 1883
  14. #if 1
  15. #define CPU_TEMP_FILE1 "/sys/class/hwmon/hwmon1/temp3_input"
  16. #else
  17. #define CPU_TEMP_FILE1 "/sys/class/therma/temp"
  18. #endif
  19. // 连接回调函数,当连接成功时会进入这里,可以在这里进行连接成功之后的操作,比如连接之后的信息同步
  20. void my_connect_callback(struct mosquitto *mosq, void *obj, int rc)
  21. {
  22. }
  23. // 断开连接回调函数,在断开连接之后进入
  24. void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result)
  25. {
  26. printf("%d:\n", __LINE__);
  27. }
  28. // 消息回调
  29. void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
  30. {
  31. time_t t;
  32. struct tm *lt;
  33. time(&t);
  34. lt = localtime(&t);
  35. printf("%d.%d.%d %d:%d:%d ", lt->tm_year + 1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
  36. printf("%d:topic(%s)->%s\n", __LINE__, (char *)msg->topic, (char *)msg->payload);
  37. }
  38. // 订阅消息回调
  39. void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
  40. {
  41. int i;
  42. time_t t;
  43. struct tm *lt;
  44. time(&t);
  45. lt = localtime(&t);
  46. printf("%d: %s\n", __LINE__, (char *)obj);
  47. printf("%d: mid=%d\n", __LINE__, mid);
  48. printf("%d: qos_count=%d\n", __LINE__, qos_count);
  49. for (i = 0; i < qos_count; i++)
  50. {
  51. printf("%d: granted_qos[%d]=%d\n", __LINE__, i, granted_qos[i]);
  52. }
  53. }
  54. /* 获取硬盘温度 */
  55. static int get_sata_hddtemp(char *device, int *value);
  56. /* 输入参数提示信息 */
  57. int print_usage(void);
  58. /* 获取cpu使用率 */
  59. double get_sysCpuUsage(void);
  60. /* 获取内存使用率 */
  61. float cal_mem_occupy(void);
  62. int main(int argc, char *argv[])
  63. {
  64. struct mosquitto *m_hMqtt;
  65. char *topic1 = "server-status";
  66. char content[256];
  67. FILE *fp = NULL;
  68. int cpu_temp = 0;
  69. int hdd_temp = 0;
  70. char *device = NULL;
  71. if (3 == argc)
  72. {
  73. if (0 != strncmp(argv[1], "-d", strlen("-d")))
  74. {
  75. print_usage();
  76. return -1;
  77. }
  78. device = argv[2];
  79. debug = 1;
  80. }
  81. else if (argc == 2)
  82. {
  83. if (0 != strncmp(argv[1], "/dev/", strlen("/dev/")))
  84. {
  85. print_usage();
  86. return -1;
  87. }
  88. device = argv[1];
  89. debug = 0;
  90. }
  91. else
  92. {
  93. print_usage();
  94. return -1;
  95. }
  96. /* 硬盘温度获取 */
  97. if (get_sata_hddtemp(device, &hdd_temp) < 0)
  98. {
  99. hdd_temp = 0;
  100. exit(-1);
  101. }
  102. //初始化lib库函数
  103. mosquitto_lib_init();
  104. // 定义一个客户端名为subtest的发布端。客户端表示订阅端的唯一性
  105. m_hMqtt = mosquitto_new("n3150-sensors", true, "data");
  106. //设置连接确认回调
  107. mosquitto_connect_callback_set(m_hMqtt, my_connect_callback);
  108. //设置断开连接确认回调
  109. mosquitto_disconnect_callback_set(m_hMqtt, my_disconnect_callback);
  110. //设置收到订阅回调
  111. //mosquitto_message_callback_set(m_hMqtt, my_message_callback);
  112. //设置订阅回调(mosquitto_subscribe()订阅后回调)
  113. //mosquitto_subscribe_callback_set(m_hMqtt, my_subscribe_callback);
  114. mosquitto_username_pw_set(m_hMqtt, "iops", "12345678");
  115. //开始连接服务器
  116. if (MOSQ_ERR_SUCCESS == mosquitto_connect(m_hMqtt, MQTT_SERVER_IP, MQTT_SERVER_PORT, 20))
  117. {
  118. printf("connect server: %s:%d success\n", MQTT_SERVER_IP, MQTT_SERVER_PORT);
  119. }
  120. else
  121. {
  122. printf("connect server: %s:%d failed\n", MQTT_SERVER_IP, MQTT_SERVER_PORT);
  123. exit(-1);
  124. }
  125. //mosquitto_subscribe(m_hMqtt,NULL,topic1,1);
  126. //mosquitto_subscribe(m_hMqtt,NULL,topic2,1);
  127. mosquitto_loop_start(m_hMqtt); //mosquitto会创建一个进程实时运行发送心跳包和接收订阅的主题消息
  128. while (1)
  129. {
  130. sleep(5);
  131. /* cpu温度获取 */
  132. fp = fopen (CPU_TEMP_FILE1, "r");
  133. if (fp < 0)
  134. {
  135. printf("open file failed,%s\n", strerror(errno));
  136. continue;
  137. }
  138. // rewind(fp);
  139. fscanf(fp, "%d", &cpu_temp);
  140. fclose(fp);
  141. /* 硬盘温度获取 */
  142. if (get_sata_hddtemp(device, &hdd_temp) < 0)
  143. {
  144. hdd_temp = 0;
  145. }
  146. /* 发送mqtt消息到服务器 */
  147. sprintf(content, "{\"cpu_temp\" : \"%d\", \"hdd_temp\" : \"%d\", \"cpu_rate\" : \"%0.2f\", \"mem_rate\" : \"%0.2f\"}",
  148. cpu_temp/1000, hdd_temp, get_sysCpuUsage(), cal_mem_occupy());
  149. mosquitto_publish(m_hMqtt, NULL, topic1, strlen(content), content, 0, false);
  150. }
  151. /* 阻塞等待 */
  152. //mosquitto_loop_stop(m_hMqtt, false);
  153. mosquitto_destroy(m_hMqtt);
  154. mosquitto_lib_cleanup();
  155. return 0;
  156. }
  157. /* 硬盘温度 */
  158. // #include <unistd.h>
  159. // #include <stdio.h>
  160. // #include <string.h>
  161. #include <getopt.h>
  162. // #include <unistd.h>
  163. // #include <stdlib.h>
  164. #include <sys/ioctl.h>
  165. #include <linux/hdreg.h>
  166. #include <scsi/scsi.h>
  167. #include <scsi/sg.h>
  168. #include <scsi/scsi_ioctl.h>
  169. #include <fcntl.h>
  170. #define DEF(X) 1
  171. #if DEF(ATA)
  172. typedef unsigned short u16;
  173. #define swapb(x) \
  174. ({ \
  175. u16 __x = (x); \
  176. (x) = ((u16)( \
  177. (((u16)(__x) & (u16)0x00ffU) << 8) | \
  178. (((u16)(__x) & (u16)0xff00U) >> 8) )); \
  179. })
  180. #define GBUF_SIZE 65535
  181. #define DEFAULT_ATTRIBUTE_ID 194
  182. #define DEFAULT_ATTRIBUTE_ID2 190
  183. #define SBUFF_SIZE 512
  184. static char sbuff[SBUFF_SIZE];
  185. static int ata_probe(int device)
  186. {
  187. if (device == -1 || ioctl(device, HDIO_GET_IDENTITY, sbuff))
  188. {
  189. return 0;
  190. }
  191. else
  192. {
  193. return 1;
  194. }
  195. }
  196. int ata_enable_smart(int device)
  197. {
  198. unsigned char cmd[4] = { WIN_SMART, 0, SMART_ENABLE, 0 };
  199. return ioctl(device, HDIO_DRIVE_CMD, cmd);
  200. }
  201. int ata_get_smart_values(int device, unsigned char *buff)
  202. {
  203. unsigned char cmd[516] = { WIN_SMART, 0, SMART_READ_VALUES, 1 };
  204. int ret;
  205. ret = ioctl(device, HDIO_DRIVE_CMD, cmd);
  206. if (ret)
  207. {
  208. return ret;
  209. }
  210. memcpy(buff, cmd + 4, 512);
  211. return 0;
  212. }
  213. static char *ata_model(int device)
  214. {
  215. if (device == -1 || ioctl(device, HDIO_GET_IDENTITY, sbuff))
  216. {
  217. return strdup("unknown");
  218. }
  219. else
  220. {
  221. return strdup((char *)((u16 *)sbuff + 27));
  222. }
  223. }
  224. unsigned char *ata_search_temperature(const unsigned char *smart_data, int attribute_id)
  225. {
  226. int i, n;
  227. n = 3;
  228. i = 0;
  229. if (debug)
  230. {
  231. printf("============ ata ============\n");
  232. }
  233. while ((debug || *(smart_data + n) != attribute_id) && i < 30)
  234. {
  235. if (debug && *(smart_data + n))
  236. printf("field(%d)\t = %d\t(0x%02x)\n",
  237. (int) * (smart_data + n),
  238. (int) * (smart_data + n + 3),
  239. *(smart_data + n + 3));
  240. n += 12;
  241. i++;
  242. }
  243. if (i >= 30)
  244. {
  245. return NULL;
  246. }
  247. else
  248. {
  249. return (unsigned char *)(smart_data + n);
  250. }
  251. }
  252. int ata_get_temperature(int fd)
  253. {
  254. unsigned char values[512]/*, thresholds[512]*/;
  255. unsigned char *field;
  256. int i;
  257. unsigned short *p;
  258. if (ata_enable_smart(fd) != 0)
  259. {
  260. printf("ATA S.M.A.R.T. not available!\n");
  261. return -1;
  262. }
  263. if (ata_get_smart_values(fd, values))
  264. {
  265. printf("ATA Enable S.M.A.R.T. err!\n");
  266. return -1;
  267. }
  268. p = (u16 *)values;
  269. for (i = 0; i < 256; i++)
  270. {
  271. swapb(*(p + i));
  272. }
  273. /* get SMART threshold values */
  274. /*
  275. if(get_smart_threshold_values(fd, thresholds)) {
  276. perror("ioctl");
  277. exit(3);
  278. }
  279. p = (u16*)thresholds;
  280. for(i = 0; i < 256; i++) {
  281. swapb(*(p+i));
  282. }
  283. */
  284. /* temperature */
  285. field = ata_search_temperature(values, DEFAULT_ATTRIBUTE_ID);
  286. if (!field)
  287. {
  288. field = ata_search_temperature(values, DEFAULT_ATTRIBUTE_ID2);
  289. }
  290. if (field)
  291. {
  292. return *(field + 3);
  293. }
  294. else
  295. {
  296. return -1;
  297. }
  298. }
  299. #endif
  300. #if DEF(SCSI)
  301. #define TEMPERATURE_PAGE 0x0d
  302. #define CDB_12_HDR_SIZE 14
  303. #define CDB_12_MAX_DATA_SIZE 0xffffffff
  304. #define CDB_6_HDR_SIZE 14
  305. #define CDB_6_MAX_DATA_SIZE 0xff
  306. #define DEXCPT_DISABLE 0xf7
  307. #define DEXCPT_ENABLE 0x08
  308. #define EWASC_ENABLE 0x10
  309. #define EWASC_DISABLE 0xef
  310. #define GBUF_SIZE 65535
  311. #define MODE_DATA_HDR_SIZE 12
  312. #define SMART_SUPPORT 0x00
  313. struct cdb10hdr
  314. {
  315. unsigned int inbufsize;
  316. unsigned int outbufsize;
  317. unsigned int cdb [10];
  318. } ;
  319. struct cdb6hdr
  320. {
  321. unsigned int inbufsize;
  322. unsigned int outbufsize;
  323. unsigned char cdb [6];
  324. };
  325. static void scsi_fixstring(unsigned char *s, int bytecount)
  326. {
  327. unsigned char *p;
  328. unsigned char *end;
  329. p = s;
  330. end = s + bytecount;
  331. /* strip leading blanks */
  332. while (s != end && *s == ' ')
  333. {
  334. ++s;
  335. }
  336. /* compress internal blanks and strip trailing blanks */
  337. while (s != end && *s)
  338. {
  339. if (*s++ != ' ' || (s != end && *s && *s != ' '))
  340. {
  341. *p++ = *(s - 1);
  342. }
  343. }
  344. /* wipe out trailing garbage */
  345. while (p != end)
  346. {
  347. *p++ = '\0';
  348. }
  349. }
  350. int scsi_SG_IO(int device, unsigned char *cdb, int cdb_len, unsigned char *buffer, int buffer_len, unsigned char *sense,
  351. unsigned char sense_len, int dxfer_direction)
  352. {
  353. struct sg_io_hdr io_hdr;
  354. memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
  355. io_hdr.interface_id = 'S';
  356. io_hdr.cmdp = cdb;
  357. io_hdr.cmd_len = cdb_len;
  358. io_hdr.dxfer_len = buffer_len;
  359. io_hdr.dxferp = buffer;
  360. io_hdr.mx_sb_len = sense_len;
  361. io_hdr.sbp = sense;
  362. io_hdr.dxfer_direction = dxfer_direction;
  363. io_hdr.timeout = 3000; /* 3 seconds should be ample */
  364. return ioctl(device, SG_IO, &io_hdr);
  365. }
  366. int scsi_SEND_COMMAND(int device, unsigned char *cdb, int cdb_len, unsigned char *buffer, int buffer_len,
  367. int dxfer_direction)
  368. {
  369. unsigned char buf[2048];
  370. unsigned int inbufsize, outbufsize, ret;
  371. switch (dxfer_direction)
  372. {
  373. case SG_DXFER_FROM_DEV:
  374. inbufsize = 0;
  375. outbufsize = buffer_len;
  376. break;
  377. case SG_DXFER_TO_DEV:
  378. inbufsize = buffer_len;
  379. outbufsize = 0;
  380. break;
  381. default:
  382. inbufsize = 0;
  383. outbufsize = 0;
  384. break;
  385. }
  386. memcpy(buf, &inbufsize, sizeof(inbufsize));
  387. memcpy(buf + sizeof(inbufsize), &outbufsize, sizeof(outbufsize));
  388. memcpy(buf + sizeof(inbufsize) + sizeof(outbufsize), cdb, cdb_len);
  389. memcpy(buf + sizeof(inbufsize) + sizeof(outbufsize) + cdb_len, buffer, buffer_len);
  390. ret = ioctl(device, SCSI_IOCTL_SEND_COMMAND, buf);
  391. memcpy(buffer, buf + sizeof(inbufsize) + sizeof(outbufsize), buffer_len);
  392. return ret;
  393. }
  394. int scsi_command(int device, unsigned char *cdb, int cdb_len, unsigned char *buffer, int buffer_len,
  395. int dxfer_direction)
  396. {
  397. static int SG_IO_supported = -1;
  398. int ret;
  399. if (SG_IO_supported == 1)
  400. {
  401. return scsi_SG_IO(device, cdb, cdb_len, buffer, buffer_len, NULL, 0, dxfer_direction);
  402. }
  403. else if (SG_IO_supported == 0)
  404. {
  405. return scsi_SEND_COMMAND(device, cdb, cdb_len, buffer, buffer_len, dxfer_direction);
  406. }
  407. else
  408. {
  409. ret = scsi_SG_IO(device, cdb, cdb_len, buffer, buffer_len, NULL, 0, dxfer_direction);
  410. if (ret == 0)
  411. {
  412. SG_IO_supported = 1;
  413. return ret;
  414. }
  415. else
  416. {
  417. SG_IO_supported = 0;
  418. return scsi_SEND_COMMAND(device, cdb, cdb_len, buffer, buffer_len, dxfer_direction);
  419. }
  420. }
  421. }
  422. int scsi_inquiry(int device, unsigned char *buffer)
  423. {
  424. unsigned char cdb[6];
  425. memset(cdb, 0, sizeof(cdb));
  426. cdb[0] = INQUIRY;
  427. cdb[4] = 36; /* should be 36 for unsafe devices (like USB mass storage stuff)
  428. * otherwise they can lock up! SPC sections 7.4 and 8.6 */
  429. if (scsi_command(device, cdb, sizeof(cdb), buffer, cdb[4], SG_DXFER_FROM_DEV) != 0)
  430. {
  431. return 1;
  432. }
  433. else
  434. {
  435. scsi_fixstring(buffer + 8, 24);
  436. return 0;
  437. }
  438. }
  439. unsigned char modesense(int device, unsigned char pagenum, unsigned char *pBuf)
  440. {
  441. unsigned char tBuf[CDB_6_MAX_DATA_SIZE + CDB_6_HDR_SIZE ];
  442. struct cdb6hdr *ioctlhdr;
  443. unsigned char status;
  444. memset(&tBuf, 0, CDB_6_MAX_DATA_SIZE + CDB_6_HDR_SIZE);
  445. ioctlhdr = (struct cdb6hdr *) &tBuf;
  446. ioctlhdr->inbufsize = 0;
  447. ioctlhdr->outbufsize = 0xff;
  448. ioctlhdr->cdb[0] = MODE_SENSE;
  449. ioctlhdr->cdb[1] = 0x00;
  450. ioctlhdr->cdb[2] = pagenum;
  451. ioctlhdr->cdb[3] = 0x00;
  452. ioctlhdr->cdb[4] = CDB_6_MAX_DATA_SIZE;
  453. ioctlhdr->cdb[5] = 0x00;
  454. status = ioctl(device, 1, &tBuf);
  455. memcpy(pBuf, &tBuf[8], 256);
  456. return status;
  457. }
  458. unsigned char modeselect(int device, unsigned char pagenum, unsigned char *pBuf)
  459. {
  460. struct cdb6hdr *ioctlhdr;
  461. unsigned char tBuf[CDB_6_MAX_DATA_SIZE + CDB_6_HDR_SIZE ];
  462. unsigned char status;
  463. memset(&tBuf, 0, CDB_6_MAX_DATA_SIZE + CDB_6_HDR_SIZE);
  464. ioctlhdr = (struct cdb6hdr *) &tBuf;
  465. ioctlhdr->inbufsize = pBuf[0] + 1;
  466. ioctlhdr->outbufsize = 0;
  467. ioctlhdr->cdb[0] = MODE_SELECT;
  468. ioctlhdr->cdb[1] = 0x11;
  469. ioctlhdr->cdb[2] = 0x00;
  470. ioctlhdr->cdb[3] = 0x00;
  471. ioctlhdr->cdb[4] = pBuf[0] + 1;
  472. ioctlhdr->cdb[5] = 0x00;
  473. tBuf[CDB_6_HDR_SIZE + 3] = 0x08;
  474. tBuf[CDB_6_HDR_SIZE + 10] = 0x02;
  475. memcpy(&tBuf[ CDB_6_HDR_SIZE + MODE_DATA_HDR_SIZE],
  476. pBuf + MODE_DATA_HDR_SIZE,
  477. pBuf[0] - MODE_DATA_HDR_SIZE + 1);
  478. tBuf[26] &= 0x3f;
  479. status = ioctl(device, 1, &tBuf);
  480. return status;
  481. }
  482. unsigned char scsi_smart_mode_page1c_handler(int device, unsigned char setting, unsigned char *retval)
  483. {
  484. char tBuf[CDB_6_MAX_DATA_SIZE];
  485. if (modesense(device, 0x1c, (unsigned char *) &tBuf) != 0)
  486. {
  487. return 1;
  488. }
  489. switch (setting)
  490. {
  491. case DEXCPT_DISABLE:
  492. tBuf[14] &= 0xf7;
  493. tBuf[15] = 0x04;
  494. break;
  495. case DEXCPT_ENABLE:
  496. tBuf[14] |= 0x08;
  497. break;
  498. case EWASC_ENABLE:
  499. tBuf[14] |= 0x10;
  500. break;
  501. case EWASC_DISABLE:
  502. tBuf[14] &= 0xef;
  503. break;
  504. case SMART_SUPPORT:
  505. *retval = tBuf[14] & 0x08;
  506. return 0;
  507. break;
  508. default:
  509. return 1;
  510. }
  511. if (modeselect(device, 0x1c, (unsigned char *) &tBuf) != 0)
  512. {
  513. return 1;
  514. }
  515. return 0;
  516. }
  517. unsigned char log_sense(int device, unsigned char pagenum, unsigned char *pBuf)
  518. {
  519. struct cdb10hdr *ioctlhdr;
  520. unsigned char tBuf[1024 + CDB_12_HDR_SIZE];
  521. unsigned char status;
  522. memset(&tBuf, 0, 255);
  523. ioctlhdr = (struct cdb10hdr *) tBuf;
  524. ioctlhdr->inbufsize = 0;
  525. ioctlhdr->outbufsize = 1024;
  526. ioctlhdr->cdb[0] = LOG_SENSE;
  527. ioctlhdr->cdb[1] = 0x00;
  528. ioctlhdr->cdb[2] = 0x40 | pagenum;
  529. ioctlhdr->cdb[3] = 0x00;
  530. ioctlhdr->cdb[4] = 0x00;
  531. ioctlhdr->cdb[5] = 0x00;
  532. ioctlhdr->cdb[6] = 0x00;
  533. ioctlhdr->cdb[7] = 0x04;
  534. ioctlhdr->cdb[8] = 0x00;
  535. ioctlhdr->cdb[9] = 0x00;
  536. status = ioctl(device, 1, &tBuf);
  537. memcpy(pBuf, &tBuf[8], 1024);
  538. return status;
  539. }
  540. static int scsi_probe(int device)
  541. {
  542. int bus_num;
  543. if (ioctl(device, SCSI_IOCTL_GET_BUS_NUMBER, &bus_num))
  544. {
  545. return 0;
  546. }
  547. else
  548. {
  549. return 1;
  550. }
  551. }
  552. static char *scsi_model(int device)
  553. {
  554. unsigned char buf[36];
  555. if (scsi_inquiry(device, buf) != 0)
  556. {
  557. return strdup("unknown");
  558. }
  559. else
  560. {
  561. return strdup(buf + 8);
  562. }
  563. }
  564. int scsi_get_temperature(int fd)
  565. {
  566. unsigned char buf[1024];
  567. unsigned char smartsupport;
  568. char gBuf[GBUF_SIZE];
  569. if (0 != scsi_smart_mode_page1c_handler(fd, SMART_SUPPORT, &smartsupport))
  570. {
  571. printf("SCSI S.M.A.R.T. not available!\n");
  572. return -1;
  573. }
  574. if (0 != scsi_smart_mode_page1c_handler(fd, DEXCPT_DISABLE, NULL))
  575. {
  576. printf("SCSI Enable S.M.A.R.T. err!\n");
  577. return -1;
  578. }
  579. if (log_sense(fd, TEMPERATURE_PAGE, buf) != 0)
  580. {
  581. printf("SCSI read err!\n");
  582. return -1;
  583. }
  584. return buf[9];
  585. }
  586. #endif
  587. #if DEF(SATA)
  588. #ifndef ATA_16
  589. /* Values for T10/04-262r7 */
  590. #define ATA_16 0x85 /* 16-byte pass-thru */
  591. #endif
  592. int sata_pass_thru(int device, unsigned char *cmd, unsigned char *buffer)
  593. {
  594. unsigned char cdb[16];
  595. unsigned char sense[32];
  596. int dxfer_direction;
  597. int ret;
  598. memset(cdb, 0, sizeof(cdb));
  599. cdb[0] = ATA_16;
  600. if (cmd[3])
  601. {
  602. cdb[1] = (4 << 1); /* PIO Data-in */
  603. cdb[2] = 0x2e; /* no off.line, cc, read from dev, lock count in sector count field */
  604. dxfer_direction = SG_DXFER_FROM_DEV;
  605. }
  606. else
  607. {
  608. cdb[1] = (3 << 1); /* Non-data */
  609. cdb[2] = 0x20; /* cc */
  610. dxfer_direction = SG_DXFER_NONE;
  611. }
  612. cdb[4] = cmd[2];
  613. if (cmd[0] == WIN_SMART)
  614. {
  615. cdb[6] = cmd[3];
  616. cdb[8] = cmd[1];
  617. cdb[10] = 0x4f;
  618. cdb[12] = 0xc2;
  619. }
  620. else
  621. {
  622. cdb[6] = cmd[1];
  623. }
  624. cdb[14] = cmd[0];
  625. ret = scsi_SG_IO(device, cdb, sizeof(cdb), buffer, cmd[3] * 512, sense, sizeof(sense), dxfer_direction);
  626. /* Verify SATA magics */
  627. if (sense[0] != 0x72)
  628. {
  629. return 1;
  630. }
  631. else
  632. {
  633. return ret;
  634. }
  635. }
  636. void sata_fixstring(unsigned char *s, int bytecount)
  637. {
  638. unsigned char *p;
  639. unsigned char *end;
  640. p = s;
  641. end = &s[bytecount & ~1]; /* bytecount must be even */
  642. /* convert from big-endian to host byte order */
  643. for (p = end ; p != s;)
  644. {
  645. unsigned short *pp = (unsigned short *)(p -= 2);
  646. *pp = ntohs(*pp);
  647. }
  648. /* strip leading blanks */
  649. while (s != end && *s == ' ')
  650. {
  651. ++s;
  652. }
  653. /* compress internal blanks and strip trailing blanks */
  654. while (s != end && *s)
  655. {
  656. if (*s++ != ' ' || (s != end && *s && *s != ' '))
  657. {
  658. *p++ = *(s - 1);
  659. }
  660. }
  661. /* wipe out trailing garbage */
  662. while (p != end)
  663. {
  664. *p++ = '\0';
  665. }
  666. }
  667. static int sata_probe(int device)
  668. {
  669. int bus_num;
  670. unsigned char cmd[4] = { WIN_IDENTIFY, 0, 0, 1 };
  671. unsigned char identify[512];
  672. char buf[36]; /* should be 36 for unsafe devices (like USB mass storage stuff)
  673. otherwise they can lock up! SPC sections 7.4 and 8.6 */
  674. /* SATA disks are difficult to detect as they answer to both ATA and SCSI
  675. commands */
  676. /* First check that the device is accessible through SCSI */
  677. if (ioctl(device, SCSI_IOCTL_GET_BUS_NUMBER, &bus_num))
  678. {
  679. return 0;
  680. }
  681. /* Get SCSI name and verify it starts with "ATA " */
  682. if (scsi_inquiry(device, buf))
  683. {
  684. return 0;
  685. }
  686. else if (strncmp(buf + 8, "ATA ", 4))
  687. {
  688. return 0;
  689. }
  690. /* Verify that it supports ATA pass thru */
  691. if (sata_pass_thru(device, cmd, identify) != 0)
  692. {
  693. return 0;
  694. }
  695. else
  696. {
  697. return 1;
  698. }
  699. }
  700. int sata_enable_smart(int device)
  701. {
  702. unsigned char cmd[4] = { WIN_SMART, 0, SMART_ENABLE, 0 };
  703. return sata_pass_thru(device, cmd, NULL);
  704. }
  705. int sata_get_smart_values(int device, unsigned char *buff)
  706. {
  707. unsigned char cmd[4] = { WIN_SMART, 0, SMART_READ_VALUES, 1 };
  708. return sata_pass_thru(device, cmd, buff);
  709. }
  710. static char *sata_model(int device)
  711. {
  712. unsigned char cmd[4] = { WIN_IDENTIFY, 0, 0, 1 };
  713. unsigned char identify[512];
  714. if (device == -1 || sata_pass_thru(device, cmd, identify))
  715. {
  716. return strdup("unknown");
  717. }
  718. else
  719. {
  720. sata_fixstring(identify + 54, 40);
  721. return strdup(identify + 54);
  722. }
  723. }
  724. static unsigned char *sata_search_temperature(const unsigned char *smart_data, int attribute_id)
  725. {
  726. int i, n;
  727. n = 3;
  728. i = 0;
  729. if (debug)
  730. {
  731. printf("============ sata ============\n");
  732. }
  733. while ((debug || *(smart_data + n) != attribute_id) && i < 30)
  734. {
  735. if (debug && *(smart_data + n))
  736. {
  737. printf("field(%d)\t = %d\t(0x%02x)\n", *(smart_data + n), *(smart_data + n + 3), *(smart_data + n + 3));
  738. }
  739. n += 12;
  740. i++;
  741. }
  742. if (i >= 30)
  743. {
  744. return NULL;
  745. }
  746. else
  747. {
  748. return (unsigned char *)(smart_data + n);
  749. }
  750. }
  751. int sata_get_temperature(int fd)
  752. {
  753. unsigned char values[512];
  754. unsigned char *field;
  755. int i;
  756. u16 *p;
  757. /* get SMART values */
  758. if (sata_enable_smart(fd) != 0)
  759. {
  760. printf("SATA S.M.A.R.T. not available!\n");
  761. return -1;
  762. }
  763. if (sata_get_smart_values(fd, values))
  764. {
  765. printf("SATA Enable S.M.A.R.T. err!\n");
  766. return -1;
  767. }
  768. p = (u16 *)values;
  769. for (i = 0; i < 256; i++)
  770. {
  771. swapb(*(p + i));
  772. }
  773. /* temperature */
  774. field = sata_search_temperature(values, DEFAULT_ATTRIBUTE_ID);
  775. if (!field)
  776. {
  777. field = sata_search_temperature(values, DEFAULT_ATTRIBUTE_ID2);
  778. }
  779. if (field)
  780. {
  781. return *(field + 3);
  782. }
  783. else
  784. {
  785. return -1;
  786. }
  787. }
  788. #endif
  789. /* 输入参数提示信息 */
  790. int print_usage(void)
  791. {
  792. printf("Usage:\n");
  793. printf(" satatemp [-d] /dev/sda\n");
  794. printf(" -d print debug info\n");
  795. return 0;
  796. }
  797. /* 获取硬盘温度 */
  798. int get_sata_hddtemp(char *device, int *value)
  799. {
  800. // int fd = 0;
  801. //int value = -1;
  802. // char type[16] = "";
  803. // char *mode = NULL;
  804. /*
  805. char *device = NULL;
  806. if (3 == argc)
  807. {
  808. if (0 != strncmp(argv[1], "-d", strlen("-d")))
  809. {
  810. print_usage();
  811. return 0;
  812. }
  813. device = argv[2];
  814. debug = 1;
  815. }
  816. else if (argc == 2)
  817. {
  818. if (0 != strncmp(argv[1], "/dev/", strlen("/dev/")))
  819. {
  820. print_usage();
  821. return 0;
  822. }
  823. device = argv[1];
  824. debug = 0;
  825. }
  826. else
  827. {
  828. print_usage();
  829. return 0;
  830. }
  831. */
  832. int fd = 0;
  833. fd = open(device, O_RDONLY | O_NONBLOCK);
  834. if (fd < 0)
  835. {
  836. printf("open hdd device err!\n");
  837. return (-1);
  838. }
  839. if (sata_probe(fd))
  840. {
  841. *value = sata_get_temperature(fd);
  842. // memset(type, 0, sizeof(type));
  843. // strcpy(type, "SATA mode");
  844. // mode = sata_model(fd);
  845. }
  846. else if (ata_probe(fd))
  847. {
  848. *value = ata_get_temperature(fd);
  849. // memset(type, 0, sizeof(type));
  850. // strcpy(type, "ATA mode");
  851. // mode = ata_model(fd);
  852. }
  853. else if (scsi_probe(fd))
  854. {
  855. *value = scsi_get_temperature(fd);
  856. // memset(type, 0, sizeof(type));
  857. // strcpy(type, "SCSI mode");
  858. // mode = scsi_model(fd);
  859. }
  860. if (*value < 0)
  861. {
  862. return -1;
  863. }
  864. // if (value > 0)
  865. // {
  866. // // printf("%s: %s, temperature: %d C\n", type, mode, value);
  867. // }
  868. // if (mode)
  869. // {
  870. // printf("%s: %s: no sensor\n", device, mode);
  871. // }
  872. // else
  873. // {
  874. // // printf("get temperature failed!\n");
  875. // }
  876. close(fd);
  877. // free(mode);
  878. return 0;
  879. }
  880. /* 获取cpu使用率 */
  881. typedef struct cpu_occupy_ //定义一个cpu occupy的结构体
  882. {
  883. char name[20]; //定义一个char类型的数组名name有20个元素
  884. unsigned int user; //定义一个无符号的int类型的user
  885. unsigned int nice; //定义一个无符号的int类型的nice
  886. unsigned int system; //定义一个无符号的int类型的system
  887. unsigned int idle; //定义一个无符号的int类型的idle
  888. unsigned int iowait;
  889. unsigned int irq;
  890. unsigned int softirq;
  891. }cpu_occupy_t;
  892. double cal_cpuoccupy (cpu_occupy_t *o, cpu_occupy_t *n)
  893. {
  894. double od, nd;
  895. double id, sd;
  896. double cpu_use ;
  897. od = (double) (o->user + o->nice + o->system +o->idle+o->softirq+o->iowait+o->irq);//第一次(用户+优先级+系统+空闲)的时间再赋给od
  898. nd = (double) (n->user + n->nice + n->system +n->idle+n->softirq+n->iowait+n->irq);//第二次(用户+优先级+系统+空闲)的时间再赋给od
  899. id = (double) (n->idle); //用户第一次和第二次的时间之差再赋给id
  900. sd = (double) (o->idle) ; //系统第一次和第二次的时间之差再赋给sd
  901. if((nd-od) != 0)
  902. cpu_use =100.0 - ((id-sd))/(nd-od)*100.00; //((用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used
  903. else
  904. cpu_use = 0;
  905. return cpu_use;
  906. }
  907. void get_cpuoccupy (cpu_occupy_t *cpust)
  908. {
  909. FILE *fd;
  910. int n;
  911. char buff[256];
  912. cpu_occupy_t *cpu_occupy;
  913. cpu_occupy=cpust;
  914. fd = fopen ("/proc/stat", "r");
  915. if(fd == NULL)
  916. {
  917. perror("fopen:");
  918. exit (0);
  919. }
  920. fgets (buff, sizeof(buff), fd);
  921. sscanf (buff, "%s %u %u %u %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice,&cpu_occupy->system,
  922. &cpu_occupy->idle ,&cpu_occupy->iowait,&cpu_occupy->irq,&cpu_occupy->softirq);
  923. fclose(fd);
  924. }
  925. double get_sysCpuUsage(void)
  926. {
  927. cpu_occupy_t cpu_stat1;
  928. cpu_occupy_t cpu_stat2;
  929. double cpu;
  930. get_cpuoccupy((cpu_occupy_t *)&cpu_stat1);
  931. sleep(1);
  932. //第二次获取cpu使用情况
  933. get_cpuoccupy((cpu_occupy_t *)&cpu_stat2);
  934. //计算cpu使用率
  935. cpu = cal_cpuoccupy ((cpu_occupy_t *)&cpu_stat1, (cpu_occupy_t *)&cpu_stat2);
  936. return cpu;
  937. }
  938. /* 获取内存使用率 */
  939. struct MEM_INFO
  940. {
  941. unsigned int total;
  942. unsigned int free;
  943. unsigned int buffers;
  944. unsigned int cached;
  945. unsigned int swap_cached;
  946. unsigned int swap_total;
  947. unsigned int swap_free;
  948. unsigned int available;
  949. };
  950. typedef struct MEM_INFO Mem_info;
  951. void get_mem_occupy (Mem_info *o)
  952. {
  953. FILE* fpMemInfo = fopen("/proc/meminfo", "r");
  954. if (NULL == fpMemInfo)
  955. {
  956. return ;
  957. }
  958. int i = 0;
  959. int value;
  960. char name[1024];
  961. char line[1024];
  962. int nFiledNumber = 2;
  963. int nMemberNumber = 5;
  964. while (fgets(line, sizeof(line) - 1, fpMemInfo))
  965. {
  966. if (sscanf(line, "%s%u", name, &value) != nFiledNumber)
  967. {
  968. continue;
  969. }
  970. if (0 == strcmp(name, "MemTotal:"))
  971. {
  972. ++i;
  973. o->total = value;
  974. }
  975. else if (0 == strcmp(name, "MemFree:"))
  976. {
  977. ++i;
  978. o->free = value;
  979. }
  980. else if (0 == strcmp(name, "MemAvailable:"))
  981. {
  982. ++i;
  983. o->available = value;
  984. }
  985. else if (0 == strcmp(name, "Buffers:"))
  986. {
  987. ++i;
  988. o->buffers = value;
  989. }
  990. else if (0 == strcmp(name, "Cached:"))
  991. {
  992. ++i;
  993. o->cached = value;
  994. }
  995. if (i == nMemberNumber)
  996. {
  997. break;
  998. }
  999. }
  1000. // system("free");
  1001. // system("cat /proc/meminfo");
  1002. // printf("MemTotal : %d\n",o->total);
  1003. // printf("MemFree : %d\n",o->free);
  1004. // printf("MemAvailable : %d\n",o->available);
  1005. // printf("MemBuffers : %d\n",o->buffers);
  1006. // printf("MemCached : %d\n",o->cached);
  1007. // printf("MemSwapCached : %d\n",o->swap_cached);
  1008. // printf("MemSwapTotal : %d\n",o->swap_total);
  1009. // printf("MemSwapFree : %d\n",o->swap_free);
  1010. fclose(fpMemInfo);
  1011. }
  1012. Mem_info omem;
  1013. float cal_mem_occupy(void)
  1014. {
  1015. get_mem_occupy(&omem);
  1016. return (100.0 * (omem.total - omem.available) / omem.total);
  1017. }