Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

hardware_info.c 23KB

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