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

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