| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194 |
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <mosquitto.h>
- #include <time.h>
- #include <unistd.h>
- #include <string.h>
- #include <errno.h>
-
- static int debug = 0;
- #define MQTT_SERVER_IP "127.0.0.1"
- #define MQTT_SERVER_PORT 1883
- #define CPU_TEMP_FILE1 "/sys/class/hwmon/hwmon1/temp3_input"
-
- // 连接回调函数,当连接成功时会进入这里,可以在这里进行连接成功之后的操作,比如连接之后的信息同步
- void my_connect_callback(struct mosquitto *mosq, void *obj, int rc)
- {
- }
-
- // 断开连接回调函数,在断开连接之后进入
- void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result)
- {
- printf("%d:\n", __LINE__);
- }
-
- // 消息回调
- void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
- {
- time_t t;
- struct tm *lt;
- time(&t);
- lt = localtime(&t);
- 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);
- printf("%d:topic(%s)->%s\n", __LINE__, (char *)msg->topic, (char *)msg->payload);
- }
-
- // 订阅消息回调
- void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
- {
- int i;
- time_t t;
- struct tm *lt;
- time(&t);
- lt = localtime(&t);
- printf("%d: %s\n", __LINE__, (char *)obj);
- printf("%d: mid=%d\n", __LINE__, mid);
- printf("%d: qos_count=%d\n", __LINE__, qos_count);
-
- for (i = 0; i < qos_count; i++)
- {
- printf("%d: granted_qos[%d]=%d\n", __LINE__, i, granted_qos[i]);
- }
- }
-
- /* 获取硬盘温度 */
- static int get_sata_hddtemp(char *device, int *value);
- /* 输入参数提示信息 */
- int print_usage(void);
- /* 获取cpu使用率 */
- double get_sysCpuUsage(void);
- /* 获取内存使用率 */
- float cal_mem_occupy(void);
-
- int main(int argc, char *argv[])
- {
- struct mosquitto *m_hMqtt;
- char *topic1 = "server-status";
- char content[256];
- FILE *fp = NULL;
- int cpu_temp = 0;
- int hdd_temp = 0;
-
- char *device = NULL;
-
- if (3 == argc)
- {
- if (0 != strncmp(argv[1], "-d", strlen("-d")))
- {
- print_usage();
- return -1;
- }
-
- device = argv[2];
- debug = 1;
- }
- else if (argc == 2)
- {
- if (0 != strncmp(argv[1], "/dev/", strlen("/dev/")))
- {
- print_usage();
- return -1;
- }
-
- device = argv[1];
- debug = 0;
- }
- else
- {
- print_usage();
- return -1;
- }
-
- /* 硬盘温度获取 */
- if (get_sata_hddtemp(device, &hdd_temp) < 0)
- {
- hdd_temp = 0;
- exit(-1);
- }
-
- //初始化lib库函数
- mosquitto_lib_init();
- // 定义一个客户端名为subtest的发布端。客户端表示订阅端的唯一性
- m_hMqtt = mosquitto_new("n3150-sensors", true, "data");
- //设置连接确认回调
- mosquitto_connect_callback_set(m_hMqtt, my_connect_callback);
- //设置断开连接确认回调
- mosquitto_disconnect_callback_set(m_hMqtt, my_disconnect_callback);
- //设置收到订阅回调
- //mosquitto_message_callback_set(m_hMqtt, my_message_callback);
- //设置订阅回调(mosquitto_subscribe()订阅后回调)
- //mosquitto_subscribe_callback_set(m_hMqtt, my_subscribe_callback);
-
- mosquitto_username_pw_set(m_hMqtt, "iops", "12345678");
-
- //开始连接服务器
- if (MOSQ_ERR_SUCCESS == mosquitto_connect(m_hMqtt, MQTT_SERVER_IP, MQTT_SERVER_PORT, 20))
- {
- printf("connect server: %s:%d success\n", MQTT_SERVER_IP, MQTT_SERVER_PORT);
- }
- else
- {
- printf("connect server: %s:%d failed\n", MQTT_SERVER_IP, MQTT_SERVER_PORT);
- exit(-1);
- }
-
- //mosquitto_subscribe(m_hMqtt,NULL,topic1,1);
- //mosquitto_subscribe(m_hMqtt,NULL,topic2,1);
- mosquitto_loop_start(m_hMqtt);
- while (1)
- {
- sleep(5);
- /* cpu温度获取 */
- fp = fopen (CPU_TEMP_FILE1, "r");
- if (fp < 0)
- {
- printf("open file failed,%s\n", strerror(errno));
- continue;
- }
- // rewind(fp);
- fscanf(fp, "%d", &cpu_temp);
- fclose(fp);
-
- /* 硬盘温度获取 */
- if (get_sata_hddtemp(device, &hdd_temp) < 0)
- {
- hdd_temp = 0;
- }
-
- /* 发送mqtt消息到服务器 */
- sprintf(content, "{\"cpu_temp\" : \"%d\", \"hdd_temp\" : \"%d\", \"cpu_rate\" : \"%0.2f\", \"mem_rate\" : \"%0.2f\"}",
- cpu_temp/1000, hdd_temp, get_sysCpuUsage(), cal_mem_occupy());
- mosquitto_publish(m_hMqtt, NULL, topic1, strlen(content), content, 0, false);
- }
-
- /* 阻塞等待 */
- //mosquitto_loop_stop(m_hMqtt, false);
- mosquitto_destroy(m_hMqtt);
- mosquitto_lib_cleanup();
- return 0;
- }
-
-
-
- /* 硬盘温度 */
- // #include <unistd.h>
- // #include <stdio.h>
- // #include <string.h>
- #include <getopt.h>
- // #include <unistd.h>
- // #include <stdlib.h>
- #include <sys/ioctl.h>
- #include <linux/hdreg.h>
- #include <scsi/scsi.h>
- #include <scsi/sg.h>
- #include <scsi/scsi_ioctl.h>
- #include <fcntl.h>
-
- #define DEF(X) 1
-
- #if DEF(ATA)
- typedef unsigned short u16;
- #define swapb(x) \
- ({ \
- u16 __x = (x); \
- (x) = ((u16)( \
- (((u16)(__x) & (u16)0x00ffU) << 8) | \
- (((u16)(__x) & (u16)0xff00U) >> 8) )); \
- })
- #define GBUF_SIZE 65535
- #define DEFAULT_ATTRIBUTE_ID 194
- #define DEFAULT_ATTRIBUTE_ID2 190
- #define SBUFF_SIZE 512
- static char sbuff[SBUFF_SIZE];
-
-
- static int ata_probe(int device)
- {
- if (device == -1 || ioctl(device, HDIO_GET_IDENTITY, sbuff))
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
-
-
-
- int ata_enable_smart(int device)
- {
- unsigned char cmd[4] = { WIN_SMART, 0, SMART_ENABLE, 0 };
- return ioctl(device, HDIO_DRIVE_CMD, cmd);
- }
-
-
- int ata_get_smart_values(int device, unsigned char *buff)
- {
- unsigned char cmd[516] = { WIN_SMART, 0, SMART_READ_VALUES, 1 };
- int ret;
- ret = ioctl(device, HDIO_DRIVE_CMD, cmd);
-
- if (ret)
- {
- return ret;
- }
-
- memcpy(buff, cmd + 4, 512);
- return 0;
- }
-
- static char *ata_model(int device)
- {
- if (device == -1 || ioctl(device, HDIO_GET_IDENTITY, sbuff))
- {
- return strdup("unknown");
- }
- else
- {
- return strdup((char *)((u16 *)sbuff + 27));
- }
- }
-
-
-
- unsigned char *ata_search_temperature(const unsigned char *smart_data, int attribute_id)
- {
- int i, n;
- n = 3;
- i = 0;
-
- if (debug)
- {
- printf("============ ata ============\n");
- }
-
- while ((debug || *(smart_data + n) != attribute_id) && i < 30)
- {
- if (debug && *(smart_data + n))
- printf("field(%d)\t = %d\t(0x%02x)\n",
- (int) * (smart_data + n),
- (int) * (smart_data + n + 3),
- *(smart_data + n + 3));
-
- n += 12;
- i++;
- }
-
- if (i >= 30)
- {
- return NULL;
- }
- else
- {
- return (unsigned char *)(smart_data + n);
- }
- }
-
-
- int ata_get_temperature(int fd)
- {
- unsigned char values[512]/*, thresholds[512]*/;
- unsigned char *field;
- int i;
- unsigned short *p;
-
- if (ata_enable_smart(fd) != 0)
- {
- printf("ATA S.M.A.R.T. not available!\n");
- return -1;
- }
-
- if (ata_get_smart_values(fd, values))
- {
- printf("ATA Enable S.M.A.R.T. err!\n");
- return -1;
- }
-
- p = (u16 *)values;
-
- for (i = 0; i < 256; i++)
- {
- swapb(*(p + i));
- }
-
- /* get SMART threshold values */
- /*
- if(get_smart_threshold_values(fd, thresholds)) {
- perror("ioctl");
- exit(3);
- }
-
- p = (u16*)thresholds;
- for(i = 0; i < 256; i++) {
- swapb(*(p+i));
- }
- */
- /* temperature */
- field = ata_search_temperature(values, DEFAULT_ATTRIBUTE_ID);
-
- if (!field)
- {
- field = ata_search_temperature(values, DEFAULT_ATTRIBUTE_ID2);
- }
-
- if (field)
- {
- return *(field + 3);
- }
- else
- {
- return -1;
- }
- }
-
- #endif
-
-
- #if DEF(SCSI)
- #define TEMPERATURE_PAGE 0x0d
- #define CDB_12_HDR_SIZE 14
- #define CDB_12_MAX_DATA_SIZE 0xffffffff
- #define CDB_6_HDR_SIZE 14
- #define CDB_6_MAX_DATA_SIZE 0xff
-
- #define DEXCPT_DISABLE 0xf7
- #define DEXCPT_ENABLE 0x08
- #define EWASC_ENABLE 0x10
- #define EWASC_DISABLE 0xef
- #define GBUF_SIZE 65535
- #define MODE_DATA_HDR_SIZE 12
- #define SMART_SUPPORT 0x00
-
-
- struct cdb10hdr
- {
- unsigned int inbufsize;
- unsigned int outbufsize;
- unsigned int cdb [10];
- } ;
-
-
-
- struct cdb6hdr
- {
- unsigned int inbufsize;
- unsigned int outbufsize;
- unsigned char cdb [6];
- };
-
-
-
- static void scsi_fixstring(unsigned char *s, int bytecount)
- {
- unsigned char *p;
- unsigned char *end;
- p = s;
- end = s + bytecount;
-
- /* strip leading blanks */
- while (s != end && *s == ' ')
- {
- ++s;
- }
-
- /* compress internal blanks and strip trailing blanks */
- while (s != end && *s)
- {
- if (*s++ != ' ' || (s != end && *s && *s != ' '))
- {
- *p++ = *(s - 1);
- }
- }
-
- /* wipe out trailing garbage */
- while (p != end)
- {
- *p++ = '\0';
- }
- }
-
- int scsi_SG_IO(int device, unsigned char *cdb, int cdb_len, unsigned char *buffer, int buffer_len, unsigned char *sense,
- unsigned char sense_len, int dxfer_direction)
- {
- struct sg_io_hdr io_hdr;
- memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
- io_hdr.interface_id = 'S';
- io_hdr.cmdp = cdb;
- io_hdr.cmd_len = cdb_len;
- io_hdr.dxfer_len = buffer_len;
- io_hdr.dxferp = buffer;
- io_hdr.mx_sb_len = sense_len;
- io_hdr.sbp = sense;
- io_hdr.dxfer_direction = dxfer_direction;
- io_hdr.timeout = 3000; /* 3 seconds should be ample */
- return ioctl(device, SG_IO, &io_hdr);
- }
-
- int scsi_SEND_COMMAND(int device, unsigned char *cdb, int cdb_len, unsigned char *buffer, int buffer_len,
- int dxfer_direction)
- {
- unsigned char buf[2048];
- unsigned int inbufsize, outbufsize, ret;
-
- switch (dxfer_direction)
- {
- case SG_DXFER_FROM_DEV:
- inbufsize = 0;
- outbufsize = buffer_len;
- break;
-
- case SG_DXFER_TO_DEV:
- inbufsize = buffer_len;
- outbufsize = 0;
- break;
-
- default:
- inbufsize = 0;
- outbufsize = 0;
- break;
- }
-
- memcpy(buf, &inbufsize, sizeof(inbufsize));
- memcpy(buf + sizeof(inbufsize), &outbufsize, sizeof(outbufsize));
- memcpy(buf + sizeof(inbufsize) + sizeof(outbufsize), cdb, cdb_len);
- memcpy(buf + sizeof(inbufsize) + sizeof(outbufsize) + cdb_len, buffer, buffer_len);
- ret = ioctl(device, SCSI_IOCTL_SEND_COMMAND, buf);
- memcpy(buffer, buf + sizeof(inbufsize) + sizeof(outbufsize), buffer_len);
- return ret;
- }
-
- int scsi_command(int device, unsigned char *cdb, int cdb_len, unsigned char *buffer, int buffer_len,
- int dxfer_direction)
- {
- static int SG_IO_supported = -1;
- int ret;
-
- if (SG_IO_supported == 1)
- {
- return scsi_SG_IO(device, cdb, cdb_len, buffer, buffer_len, NULL, 0, dxfer_direction);
- }
- else if (SG_IO_supported == 0)
- {
- return scsi_SEND_COMMAND(device, cdb, cdb_len, buffer, buffer_len, dxfer_direction);
- }
- else
- {
- ret = scsi_SG_IO(device, cdb, cdb_len, buffer, buffer_len, NULL, 0, dxfer_direction);
-
- if (ret == 0)
- {
- SG_IO_supported = 1;
- return ret;
- }
- else
- {
- SG_IO_supported = 0;
- return scsi_SEND_COMMAND(device, cdb, cdb_len, buffer, buffer_len, dxfer_direction);
- }
- }
- }
-
- int scsi_inquiry(int device, unsigned char *buffer)
- {
- unsigned char cdb[6];
- memset(cdb, 0, sizeof(cdb));
- cdb[0] = INQUIRY;
- cdb[4] = 36; /* should be 36 for unsafe devices (like USB mass storage stuff)
- * otherwise they can lock up! SPC sections 7.4 and 8.6 */
-
- if (scsi_command(device, cdb, sizeof(cdb), buffer, cdb[4], SG_DXFER_FROM_DEV) != 0)
- {
- return 1;
- }
- else
- {
- scsi_fixstring(buffer + 8, 24);
- return 0;
- }
- }
-
-
-
- unsigned char modesense(int device, unsigned char pagenum, unsigned char *pBuf)
- {
- unsigned char tBuf[CDB_6_MAX_DATA_SIZE + CDB_6_HDR_SIZE ];
- struct cdb6hdr *ioctlhdr;
- unsigned char status;
- memset(&tBuf, 0, CDB_6_MAX_DATA_SIZE + CDB_6_HDR_SIZE);
- ioctlhdr = (struct cdb6hdr *) &tBuf;
- ioctlhdr->inbufsize = 0;
- ioctlhdr->outbufsize = 0xff;
- ioctlhdr->cdb[0] = MODE_SENSE;
- ioctlhdr->cdb[1] = 0x00;
- ioctlhdr->cdb[2] = pagenum;
- ioctlhdr->cdb[3] = 0x00;
- ioctlhdr->cdb[4] = CDB_6_MAX_DATA_SIZE;
- ioctlhdr->cdb[5] = 0x00;
- status = ioctl(device, 1, &tBuf);
- memcpy(pBuf, &tBuf[8], 256);
- return status;
- }
-
-
-
-
- unsigned char modeselect(int device, unsigned char pagenum, unsigned char *pBuf)
- {
- struct cdb6hdr *ioctlhdr;
- unsigned char tBuf[CDB_6_MAX_DATA_SIZE + CDB_6_HDR_SIZE ];
- unsigned char status;
- memset(&tBuf, 0, CDB_6_MAX_DATA_SIZE + CDB_6_HDR_SIZE);
- ioctlhdr = (struct cdb6hdr *) &tBuf;
- ioctlhdr->inbufsize = pBuf[0] + 1;
- ioctlhdr->outbufsize = 0;
- ioctlhdr->cdb[0] = MODE_SELECT;
- ioctlhdr->cdb[1] = 0x11;
- ioctlhdr->cdb[2] = 0x00;
- ioctlhdr->cdb[3] = 0x00;
- ioctlhdr->cdb[4] = pBuf[0] + 1;
- ioctlhdr->cdb[5] = 0x00;
- tBuf[CDB_6_HDR_SIZE + 3] = 0x08;
- tBuf[CDB_6_HDR_SIZE + 10] = 0x02;
- memcpy(&tBuf[ CDB_6_HDR_SIZE + MODE_DATA_HDR_SIZE],
- pBuf + MODE_DATA_HDR_SIZE,
- pBuf[0] - MODE_DATA_HDR_SIZE + 1);
- tBuf[26] &= 0x3f;
- status = ioctl(device, 1, &tBuf);
- return status;
- }
-
-
-
- unsigned char scsi_smart_mode_page1c_handler(int device, unsigned char setting, unsigned char *retval)
- {
- char tBuf[CDB_6_MAX_DATA_SIZE];
-
- if (modesense(device, 0x1c, (unsigned char *) &tBuf) != 0)
- {
- return 1;
- }
-
- switch (setting)
- {
- case DEXCPT_DISABLE:
- tBuf[14] &= 0xf7;
- tBuf[15] = 0x04;
- break;
-
- case DEXCPT_ENABLE:
- tBuf[14] |= 0x08;
- break;
-
- case EWASC_ENABLE:
- tBuf[14] |= 0x10;
- break;
-
- case EWASC_DISABLE:
- tBuf[14] &= 0xef;
- break;
-
- case SMART_SUPPORT:
- *retval = tBuf[14] & 0x08;
- return 0;
- break;
-
- default:
- return 1;
- }
-
- if (modeselect(device, 0x1c, (unsigned char *) &tBuf) != 0)
- {
- return 1;
- }
-
- return 0;
- }
-
-
-
-
- unsigned char log_sense(int device, unsigned char pagenum, unsigned char *pBuf)
- {
- struct cdb10hdr *ioctlhdr;
- unsigned char tBuf[1024 + CDB_12_HDR_SIZE];
- unsigned char status;
- memset(&tBuf, 0, 255);
- ioctlhdr = (struct cdb10hdr *) tBuf;
- ioctlhdr->inbufsize = 0;
- ioctlhdr->outbufsize = 1024;
- ioctlhdr->cdb[0] = LOG_SENSE;
- ioctlhdr->cdb[1] = 0x00;
- ioctlhdr->cdb[2] = 0x40 | pagenum;
- ioctlhdr->cdb[3] = 0x00;
- ioctlhdr->cdb[4] = 0x00;
- ioctlhdr->cdb[5] = 0x00;
- ioctlhdr->cdb[6] = 0x00;
- ioctlhdr->cdb[7] = 0x04;
- ioctlhdr->cdb[8] = 0x00;
- ioctlhdr->cdb[9] = 0x00;
- status = ioctl(device, 1, &tBuf);
- memcpy(pBuf, &tBuf[8], 1024);
- return status;
- }
-
- static int scsi_probe(int device)
- {
- int bus_num;
-
- if (ioctl(device, SCSI_IOCTL_GET_BUS_NUMBER, &bus_num))
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
-
- static char *scsi_model(int device)
- {
- unsigned char buf[36];
-
- if (scsi_inquiry(device, buf) != 0)
- {
- return strdup("unknown");
- }
- else
- {
- return strdup(buf + 8);
- }
- }
-
-
-
- int scsi_get_temperature(int fd)
- {
- unsigned char buf[1024];
- unsigned char smartsupport;
- char gBuf[GBUF_SIZE];
-
- if (0 != scsi_smart_mode_page1c_handler(fd, SMART_SUPPORT, &smartsupport))
- {
- printf("SCSI S.M.A.R.T. not available!\n");
- return -1;
- }
-
- if (0 != scsi_smart_mode_page1c_handler(fd, DEXCPT_DISABLE, NULL))
- {
- printf("SCSI Enable S.M.A.R.T. err!\n");
- return -1;
- }
-
- if (log_sense(fd, TEMPERATURE_PAGE, buf) != 0)
- {
- printf("SCSI read err!\n");
- return -1;
- }
-
- return buf[9];
- }
-
- #endif
-
-
-
- #if DEF(SATA)
- #ifndef ATA_16
- /* Values for T10/04-262r7 */
- #define ATA_16 0x85 /* 16-byte pass-thru */
- #endif
-
- int sata_pass_thru(int device, unsigned char *cmd, unsigned char *buffer)
- {
- unsigned char cdb[16];
- unsigned char sense[32];
- int dxfer_direction;
- int ret;
- memset(cdb, 0, sizeof(cdb));
- cdb[0] = ATA_16;
-
- if (cmd[3])
- {
- cdb[1] = (4 << 1); /* PIO Data-in */
- cdb[2] = 0x2e; /* no off.line, cc, read from dev, lock count in sector count field */
- dxfer_direction = SG_DXFER_FROM_DEV;
- }
- else
- {
- cdb[1] = (3 << 1); /* Non-data */
- cdb[2] = 0x20; /* cc */
- dxfer_direction = SG_DXFER_NONE;
- }
-
- cdb[4] = cmd[2];
-
- if (cmd[0] == WIN_SMART)
- {
- cdb[6] = cmd[3];
- cdb[8] = cmd[1];
- cdb[10] = 0x4f;
- cdb[12] = 0xc2;
- }
- else
- {
- cdb[6] = cmd[1];
- }
-
- cdb[14] = cmd[0];
- ret = scsi_SG_IO(device, cdb, sizeof(cdb), buffer, cmd[3] * 512, sense, sizeof(sense), dxfer_direction);
-
- /* Verify SATA magics */
- if (sense[0] != 0x72)
- {
- return 1;
- }
- else
- {
- return ret;
- }
- }
-
- void sata_fixstring(unsigned char *s, int bytecount)
- {
- unsigned char *p;
- unsigned char *end;
- p = s;
- end = &s[bytecount & ~1]; /* bytecount must be even */
-
- /* convert from big-endian to host byte order */
- for (p = end ; p != s;)
- {
- unsigned short *pp = (unsigned short *)(p -= 2);
- *pp = ntohs(*pp);
- }
-
- /* strip leading blanks */
- while (s != end && *s == ' ')
- {
- ++s;
- }
-
- /* compress internal blanks and strip trailing blanks */
- while (s != end && *s)
- {
- if (*s++ != ' ' || (s != end && *s && *s != ' '))
- {
- *p++ = *(s - 1);
- }
- }
-
- /* wipe out trailing garbage */
- while (p != end)
- {
- *p++ = '\0';
- }
- }
-
- static int sata_probe(int device)
- {
- int bus_num;
- unsigned char cmd[4] = { WIN_IDENTIFY, 0, 0, 1 };
- unsigned char identify[512];
- char buf[36]; /* should be 36 for unsafe devices (like USB mass storage stuff)
- otherwise they can lock up! SPC sections 7.4 and 8.6 */
-
- /* SATA disks are difficult to detect as they answer to both ATA and SCSI
- commands */
-
- /* First check that the device is accessible through SCSI */
- if (ioctl(device, SCSI_IOCTL_GET_BUS_NUMBER, &bus_num))
- {
- return 0;
- }
-
- /* Get SCSI name and verify it starts with "ATA " */
- if (scsi_inquiry(device, buf))
- {
- return 0;
- }
- else if (strncmp(buf + 8, "ATA ", 4))
- {
- return 0;
- }
-
- /* Verify that it supports ATA pass thru */
- if (sata_pass_thru(device, cmd, identify) != 0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
-
-
- int sata_enable_smart(int device)
- {
- unsigned char cmd[4] = { WIN_SMART, 0, SMART_ENABLE, 0 };
- return sata_pass_thru(device, cmd, NULL);
- }
-
- int sata_get_smart_values(int device, unsigned char *buff)
- {
- unsigned char cmd[4] = { WIN_SMART, 0, SMART_READ_VALUES, 1 };
- return sata_pass_thru(device, cmd, buff);
- }
-
- static char *sata_model(int device)
- {
- unsigned char cmd[4] = { WIN_IDENTIFY, 0, 0, 1 };
- unsigned char identify[512];
-
- if (device == -1 || sata_pass_thru(device, cmd, identify))
- {
- return strdup("unknown");
- }
- else
- {
- sata_fixstring(identify + 54, 40);
- return strdup(identify + 54);
- }
- }
-
-
- static unsigned char *sata_search_temperature(const unsigned char *smart_data, int attribute_id)
- {
- int i, n;
- n = 3;
- i = 0;
-
- if (debug)
- {
- printf("============ sata ============\n");
- }
-
- while ((debug || *(smart_data + n) != attribute_id) && i < 30)
- {
- if (debug && *(smart_data + n))
- {
- printf("field(%d)\t = %d\t(0x%02x)\n", *(smart_data + n), *(smart_data + n + 3), *(smart_data + n + 3));
- }
-
- n += 12;
- i++;
- }
-
- if (i >= 30)
- {
- return NULL;
- }
- else
- {
- return (unsigned char *)(smart_data + n);
- }
- }
-
-
- int sata_get_temperature(int fd)
- {
- unsigned char values[512];
- unsigned char *field;
- int i;
- u16 *p;
-
- /* get SMART values */
- if (sata_enable_smart(fd) != 0)
- {
- printf("SATA S.M.A.R.T. not available!\n");
- return -1;
- }
-
- if (sata_get_smart_values(fd, values))
- {
- printf("SATA Enable S.M.A.R.T. err!\n");
- return -1;
- }
-
- p = (u16 *)values;
-
- for (i = 0; i < 256; i++)
- {
- swapb(*(p + i));
- }
-
- /* temperature */
- field = sata_search_temperature(values, DEFAULT_ATTRIBUTE_ID);
-
- if (!field)
- {
- field = sata_search_temperature(values, DEFAULT_ATTRIBUTE_ID2);
- }
-
- if (field)
- {
- return *(field + 3);
- }
- else
- {
- return -1;
- }
- }
- #endif
-
- /* 输入参数提示信息 */
- int print_usage(void)
- {
- printf("Usage:\n");
- printf(" satatemp [-d] /dev/sda\n");
- printf(" -d print debug info\n");
- return 0;
- }
-
-
- /* 获取硬盘温度 */
- int get_sata_hddtemp(char *device, int *value)
- {
- // int fd = 0;
- //int value = -1;
- // char type[16] = "";
- char *mode = NULL;
-
- /*
- char *device = NULL;
-
- if (3 == argc)
- {
- if (0 != strncmp(argv[1], "-d", strlen("-d")))
- {
- print_usage();
- return 0;
- }
-
- device = argv[2];
- debug = 1;
- }
- else if (argc == 2)
- {
- if (0 != strncmp(argv[1], "/dev/", strlen("/dev/")))
- {
- print_usage();
- return 0;
- }
-
- device = argv[1];
- debug = 0;
- }
- else
- {
- print_usage();
- return 0;
- }
- */
-
- int fd = 0;
- fd = open(device, O_RDONLY | O_NONBLOCK);
-
- if (fd < 0)
- {
- printf("open hdd device err!\n");
- return (-1);
- }
-
- if (sata_probe(fd))
- {
- *value = sata_get_temperature(fd);
- // memset(type, 0, sizeof(type));
- // strcpy(type, "SATA mode");
- // mode = sata_model(fd);
- }
- else if (ata_probe(fd))
- {
- *value = ata_get_temperature(fd);
- // memset(type, 0, sizeof(type));
- // strcpy(type, "ATA mode");
- // mode = ata_model(fd);
- }
- else if (scsi_probe(fd))
- {
- *value = scsi_get_temperature(fd);
- // memset(type, 0, sizeof(type));
- // strcpy(type, "SCSI mode");
- // mode = scsi_model(fd);
- }
-
- if (*value < 0)
- {
- return -1;
- }
-
-
- // if (value > 0)
- // {
- // // printf("%s: %s, temperature: %d C\n", type, mode, value);
- // }
- // if (mode)
- // {
- // printf("%s: %s: no sensor\n", device, mode);
- // }
- // else
- // {
- // // printf("get temperature failed!\n");
- // }
-
- close(fd);
- free(mode);
- return 0;
- }
-
-
- /* 获取cpu使用率 */
- typedef struct cpu_occupy_ //定义一个cpu occupy的结构体
- {
- char name[20]; //定义一个char类型的数组名name有20个元素
- unsigned int user; //定义一个无符号的int类型的user
- unsigned int nice; //定义一个无符号的int类型的nice
- unsigned int system; //定义一个无符号的int类型的system
- unsigned int idle; //定义一个无符号的int类型的idle
- unsigned int iowait;
- unsigned int irq;
- unsigned int softirq;
- }cpu_occupy_t;
-
- double cal_cpuoccupy (cpu_occupy_t *o, cpu_occupy_t *n)
- {
- double od, nd;
- double id, sd;
- double cpu_use ;
-
- od = (double) (o->user + o->nice + o->system +o->idle+o->softirq+o->iowait+o->irq);//第一次(用户+优先级+系统+空闲)的时间再赋给od
- nd = (double) (n->user + n->nice + n->system +n->idle+n->softirq+n->iowait+n->irq);//第二次(用户+优先级+系统+空闲)的时间再赋给od
-
- id = (double) (n->idle); //用户第一次和第二次的时间之差再赋给id
- sd = (double) (o->idle) ; //系统第一次和第二次的时间之差再赋给sd
- if((nd-od) != 0)
- cpu_use =100.0 - ((id-sd))/(nd-od)*100.00; //((用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used
- else
- cpu_use = 0;
- return cpu_use;
- }
-
- void get_cpuoccupy (cpu_occupy_t *cpust)
- {
- FILE *fd;
- int n;
- char buff[256];
- cpu_occupy_t *cpu_occupy;
- cpu_occupy=cpust;
-
- fd = fopen ("/proc/stat", "r");
- if(fd == NULL)
- {
- perror("fopen:");
- exit (0);
- }
- fgets (buff, sizeof(buff), fd);
-
- sscanf (buff, "%s %u %u %u %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice,&cpu_occupy->system,
- &cpu_occupy->idle ,&cpu_occupy->iowait,&cpu_occupy->irq,&cpu_occupy->softirq);
-
- fclose(fd);
- }
-
- double get_sysCpuUsage(void)
- {
- cpu_occupy_t cpu_stat1;
- cpu_occupy_t cpu_stat2;
- double cpu;
- get_cpuoccupy((cpu_occupy_t *)&cpu_stat1);
- sleep(1);
- //第二次获取cpu使用情况
- get_cpuoccupy((cpu_occupy_t *)&cpu_stat2);
-
- //计算cpu使用率
- cpu = cal_cpuoccupy ((cpu_occupy_t *)&cpu_stat1, (cpu_occupy_t *)&cpu_stat2);
-
- return cpu;
- }
-
- /* 获取内存使用率 */
- struct MEM_INFO
- {
- unsigned int total;
- unsigned int free;
- unsigned int buffers;
- unsigned int cached;
- unsigned int swap_cached;
- unsigned int swap_total;
- unsigned int swap_free;
- unsigned int available;
- };
- typedef struct MEM_INFO Mem_info;
-
- void get_mem_occupy (Mem_info *o)
- {
- FILE* fpMemInfo = fopen("/proc/meminfo", "r");
- if (NULL == fpMemInfo)
- {
- return ;
- }
- int i = 0;
- int value;
- char name[1024];
- char line[1024];
- int nFiledNumber = 2;
- int nMemberNumber = 5;
- while (fgets(line, sizeof(line) - 1, fpMemInfo))
- {
- if (sscanf(line, "%s%u", name, &value) != nFiledNumber)
- {
- continue;
- }
- if (0 == strcmp(name, "MemTotal:"))
- {
- ++i;
- o->total = value;
- }
- else if (0 == strcmp(name, "MemFree:"))
- {
- ++i;
- o->free = value;
- }
- else if (0 == strcmp(name, "MemAvailable:"))
- {
- ++i;
- o->available = value;
- }
- else if (0 == strcmp(name, "Buffers:"))
- {
- ++i;
- o->buffers = value;
- }
- else if (0 == strcmp(name, "Cached:"))
- {
- ++i;
- o->cached = value;
- }
- if (i == nMemberNumber)
- {
- break;
- }
- }
- // system("free");
- // system("cat /proc/meminfo");
- // printf("MemTotal : %d\n",o->total);
- // printf("MemFree : %d\n",o->free);
- // printf("MemAvailable : %d\n",o->available);
- // printf("MemBuffers : %d\n",o->buffers);
- // printf("MemCached : %d\n",o->cached);
- // printf("MemSwapCached : %d\n",o->swap_cached);
- // printf("MemSwapTotal : %d\n",o->swap_total);
- // printf("MemSwapFree : %d\n",o->swap_free);
- fclose(fpMemInfo);
- }
-
- Mem_info omem;
- float cal_mem_occupy(void)
- {
- get_mem_occupy(&omem);
- return (100.0 * (omem.total - omem.available) / omem.total);
- }
|