Sfoglia il codice sorgente

[add] 负载和进程数量

new_master
huangyulong 3 settimane fa
parent
commit
b6c5445d6a
3 ha cambiato i file con 49 aggiunte e 6 eliminazioni
  1. 35
    3
      hardware_info.c
  2. 2
    1
      hardware_info.h
  3. 12
    2
      server-monitor.c

+ 35
- 3
hardware_info.c Vedi File

@@ -1000,12 +1000,44 @@ int hardware_info_init(void)
1000 1000
 
1001 1001
 #include <sys/sysinfo.h>
1002 1002
 
1003
-long get_system_uptime(void)
1003
+#if 0
1004
+struct sysinfo 
1005
+{
1006
+    long uptime; /* Seconds since boot */
1007
+    unsigned long loads[3]; /* 1, 5, and 15 minute Load averages */
1008
+    unsigned long totalram; /* Total usable main memory size */
1009
+    unsigned long freeram; /* Available memory size */
1010
+    unsigned long sharedram; /* Amount of shared memory */
1011
+    unsigned long bufferram; /* Memory used by buffers */
1012
+    unsigned long totalswap; /* Total swap space size */
1013
+    unsigned long freeswap; /* swap space still available */
1014
+    unsigned short procs; /* Number of current processes */
1015
+    char _f[22]; /* Pads structure to 64 bytes */
1016
+};
1017
+#endif
1018
+
1019
+// #include <stdio.h>
1020
+int get_system_info(long *uptime, float *load, int *procs)
1004 1021
 {
1005 1022
     struct sysinfo info;
1006 1023
 
1007 1024
     if (sysinfo(&info))
1008
-            return -1;
1025
+        return -1;
1009 1026
 
1010
-    return info.uptime;
1027
+    if (uptime)
1028
+        *uptime = info.uptime;
1029
+    
1030
+    // if (mem_occupy) ²»¶Ô
1031
+    //     *mem_occupy = (100.0 * (info.totalram - info.freeram) / info.totalram);
1032
+
1033
+    const float load_1m = info.loads[0] / 65536.0;
1034
+
1035
+    if (load)
1036
+        *load = (load_1m / 4) * 100 ;
1037
+    
1038
+    if (procs)
1039
+        *procs = info.procs;
1040
+
1041
+    // printf("load:%f, mem_occupy:%f, totalram:%llu, freeram:%llu, procs:%d\n", *load, *mem_occupy, info.totalram, info.freeram, *procs);
1042
+    return 0;
1011 1043
 }

+ 2
- 1
hardware_info.h Vedi File

@@ -13,5 +13,6 @@ int get_cpu_temp(void);
13 13
 
14 14
 void set_debug(int data);
15 15
 
16
-long get_system_uptime(void);
16
+int get_system_info(long *uptime, float *load, int *procs);
17
+
17 18
 #endif

+ 12
- 2
server-monitor.c Vedi File

@@ -100,8 +100,18 @@ int main(int argc, char *argv[])
100 100
     while (1)
101 101
     {
102 102
         sleep(2);
103
-        sprintf(content, "{\"uptime\" : \"%d\", \"cpu_temp\" : \"%d\", \"hdd_temp\" : \"%d\", \"cpu_rate\" : \"%0.1f\", \"mem_rate\" : \"%0.1f\"}",
104
-                get_system_uptime(), get_cpu_temp(), get_sata_hddtemp(), get_sysCpuUsage(), cal_mem_occupy());
103
+        long uptime;
104
+        float load; // 1分钟的负载
105
+        int *procs;
106
+
107
+        if (get_system_info(&uptime, &load, &procs) < 0)
108
+        {
109
+            printf("get_system_info fail!\n");
110
+            continue;
111
+        }
112
+            
113
+        sprintf(content, "{\"uptime\" : \"%d\", \"load\" : \"%0.1f\", \"procs\" : \"%d\", \"cpu_temp\" : \"%d\", \"hdd_temp\" : \"%d\", \"cpu_rate\" : \"%0.1f\", \"mem_rate\" : \"%0.1f\"}",
114
+                uptime, load, procs, get_cpu_temp(), get_sata_hddtemp(), get_sysCpuUsage(), cal_mem_occupy());
105 115
         mosquitto_publish(m_hMqtt, NULL, MQTT_PUB_TOPIC, strlen(content), content, 0, false);
106 116
     }
107 117
 

Loading…
Annulla
Salva