added graph for cpu usage.
authorJean-Philippe Orsini <jeanfi@gmail.com>
Fri, 16 Sep 2011 12:59:05 +0000 (12:59 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Fri, 16 Sep 2011 12:59:05 +0000 (12:59 +0000)
NEWS
src/lib/cpu.c
src/lib/cpu.h
src/server/server.c
src/server/server.h
www/psensor.js

diff --git a/NEWS b/NEWS
index cf5a83d..dd7dcd3 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,9 @@
 * v0.6.2.11
 ** psensor-server/web interface: use jqplot default css.
 ** psensor-server/web interface: added excanvas for ie < 9 support.
+** psensor-server/web interface: automatic update of the monitoring page.
+** psensor-server/web interface: improved styling and navigation.
+** psensor-server/web interface: added graph for cpu usage.
 
 * v0.6.2.10
 ** psensor-server: added network, cpu load and memory information in
index e6ed3f3..4ec77ea 100644 (file)
@@ -31,7 +31,7 @@ static glibtop_cpu *cpu;
 static float last_used;
 static float last_total;
 
-static struct psensor *create_sensor(int measures_len)
+struct psensor *create_cpu_usage_sensor(int measures_len)
 {
        char *label;
        int type;
@@ -52,7 +52,7 @@ cpu_psensor_list_add(struct psensor **sensors, int measures_len)
 {
        struct psensor *s;
 
-       s = create_sensor(measures_len);
+       s = create_cpu_usage_sensor(measures_len);
 
        return psensor_list_add(sensors, s);
 }
@@ -81,6 +81,11 @@ static double get_usage()
        return cpu_rate;
 }
 
+void cpu_usage_sensor_update(struct psensor *s)
+{
+       psensor_set_current_value(s, get_usage());
+}
+
 void cpu_psensor_list_update(struct psensor **sensors)
 {
        struct psensor **ss, *s;
@@ -90,7 +95,7 @@ void cpu_psensor_list_update(struct psensor **sensors)
                s = *ss;
 
                if (s->type == SENSOR_TYPE_CPU_USAGE)
-                       psensor_set_current_value(s, get_usage());
+                       cpu_usage_sensor_update(s);
 
                ss++;
        }
index 3381b0e..ff373da 100644 (file)
 
 #include "psensor.h"
 
-void cpu_psensor_list_update(struct psensor **sensors);
-struct psensor **cpu_psensor_list_add(struct psensor **sensors,
+struct psensor *create_cpu_usage_sensor(int measures_len);
+
+void cpu_usage_sensor_update(struct psensor *);
+void cpu_psensor_list_update(struct psensor **);
+
+struct psensor **cpu_psensor_list_add(struct psensor **,
                                      int values_max_len);
 
 void cpu_cleanup();
index 8395609..a87ec64 100644 (file)
@@ -38,6 +38,7 @@
 
 #ifdef HAVE_GTOP
 #include "sysinfo.h"
+#include "cpu.h"
 #endif
 
 #include "psensor_json.h"
@@ -155,6 +156,8 @@ create_response_api(const char *nurl,
        } else if (!strcmp(nurl, URL_API_1_0_SYSINFO)) {
 
                page = sysinfo_to_json_string(&server_data.psysinfo);
+       } else if (!strcmp(nurl, URL_API_1_0_CPU_USAGE)) {
+               page = sensor_to_json_string(server_data.cpu_usage);
 #endif
        } else if (!strncmp(nurl, URL_BASE_API_1_0_SENSORS,
                            strlen(URL_BASE_API_1_0_SENSORS))
@@ -351,6 +354,10 @@ int main(int argc, char *argv[])
 
        server_data.sensors = get_all_sensors(600);
 
+#ifdef HAVE_GTOP
+       server_data.cpu_usage = create_cpu_usage_sensor(600);
+#endif
+
        if (!*server_data.sensors)
                fprintf(stderr, _("ERROR: no sensors detected\n"));
 
@@ -372,6 +379,7 @@ int main(int argc, char *argv[])
 
 #ifdef HAVE_GTOP
                sysinfo_update(&server_data.psysinfo);
+               cpu_usage_sensor_update(server_data.cpu_usage);
 #endif
                psensor_list_update_measures(server_data.sensors);
 
@@ -383,11 +391,15 @@ int main(int argc, char *argv[])
 
        /* sanity cleanup for valgrind */
        psensor_list_free(server_data.sensors);
+#ifdef HAVE_GTOP
+       psensor_free(server_data.cpu_usage);
+#endif
        free(server_data.www_dir);
        sensors_cleanup();
 
 #ifdef HAVE_GTOP
        sysinfo_cleanup();
+       cpu_cleanup();
 #endif
 
        return EXIT_SUCCESS;
index fd824fb..95c582d 100644 (file)
 #define URL_BASE_API_1_0_SENSORS "/api/1.0/sensors"
 #define URL_API_1_0_SERVER_STOP "/api/1.0/server/stop"
 #define URL_API_1_0_SYSINFO "/api/1.0/sysinfo"
+#define URL_API_1_0_CPU_USAGE "/api/1.0/cpu/usage"
 
 struct server_data {
+       struct psensor *cpu_usage;
        struct psensor **sensors;
        struct psysinfo psysinfo;
        char *www_dir;
index d3372d0..8a7503a 100644 (file)
@@ -54,6 +54,8 @@ function type_to_str(stype) {
         stype_str = "NVidia";
     else if (stype & 0x0400)
         stype_str = "HDD";
+    else if (stype & 0x0800)
+        stype_str = "CPU Usage Percentage";
     else if (stype & 0x1000) 
         stype_str = "AMD";
  
@@ -150,8 +152,10 @@ function update_chart(chart_id, title, data) {
 function update_menu() {
     var name, link, url, str;
 
+    str = "";
+
     $.getJSON("/api/1.0/sensors", function(data) {
-       str = "<li><em>Sensors</em>\n<ul>";
+       str += "<li><em>Sensors</em>\n<ul>";
 
        $.each(data, function(i, item) {
             name = item["name"];
@@ -159,10 +163,17 @@ function update_menu() {
            link = "<a href='"+url+"'>"+name+"</a>";
            str += "<li>"+link+"</li>";
        });
+       str += "</li></ul>";
 
-       str += "</ul>";
-
+       str += "<li><em>CPU</em><ul>";
+       url = "details.html?id="+escape("/api/1.0/cpu/usage");
+       link = "<a href='"+url+"'>usage</a>";
+       str += "<li>"+link+"</li>";
+       
+       str += "</li></ul>";
+       
        $("#menu-list").append(str);
+
     });
 
 }