Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

server-monitor.c 28KB

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