| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193 |
- #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
- #if 1
- #define CPU_TEMP_FILE1 "/sys/class/hwmon/hwmon1/temp3_input"
- #else
- #define CPU_TEMP_FILE1 "/sys/class/therma/temp"
- #endif
-
- // 连接回调函数,当连接成功时会进入这里,可以在这里进行连接成功之后的操作,比如连接之后的信息同步
- 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;
- }
-
- /* 硬盘温度获取 */
- get_sata_hddtemp(device, &hdd_temp);
-
- //初始化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); //mosquitto会创建一个进程实时运行发送心跳包和接收订阅的主题消息
- while (1)
- {
- sleep(5);
- /* cpu温度获取 */
- fp = fopen (CPU_TEMP_FILE1, "r");
- if (NULL == fp)
- {
- printf("open file failed,%s\n", strerror(errno));
- }
- else
- {
- // rewind(fp);
- fscanf(fp, "%d", &cpu_temp);
- fclose(fp);
- }
-
- /* 硬盘温度获取 */
- get_sata_hddtemp(device, &hdd_temp);
-
- /* 发送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);
- }
|