added network, cpu load and memory information in
[psensor.git] / src / server / sysinfo.c
1 /*
2     Copyright (C) 2010-2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17     02110-1301 USA
18 */
19
20 #include <stdlib.h>
21 #include <glibtop/cpu.h>
22 #include <glibtop/netlist.h>
23 #include <glibtop/netload.h>
24 #include <sys/sysinfo.h>
25
26 #include <json/json.h>
27
28 #include "sysinfo.h"
29
30 static glibtop_cpu *cpu;
31 static float last_used;
32 static float last_total;
33
34 void sysinfo_update(struct psysinfo *info)
35 {
36         unsigned long int used = 0;
37         unsigned long int dt;
38         glibtop_netlist buf;
39         char **interfaces;
40         guint32 i;
41
42         /* cpu */
43         if (!cpu)
44                 cpu = malloc(sizeof(glibtop_cpu));
45
46         glibtop_get_cpu(cpu);
47
48         used = cpu->user + cpu->nice + cpu->sys;
49
50         dt = cpu->total - last_total;
51
52         if (dt)
53                 info->cpu_rate = (used - last_used) / dt;
54
55         last_used = used;
56         last_total = cpu->total;
57
58         /* memory */
59         sysinfo(&info->sysinfo);
60
61         /* network */
62         if (!info->interfaces)
63                 info->interfaces = glibtop_get_netlist(&buf);
64 }
65
66 void sysinfo_cleanup()
67 {
68         if (cpu)
69                 free(cpu);
70 }
71
72 static json_object *ram_to_json_object(const struct sysinfo *s)
73 {
74         json_object *obj = json_object_new_object();
75
76         json_object_object_add(obj, "total",
77                                json_object_new_double(s->totalram));
78
79         json_object_object_add(obj, "free",
80                                json_object_new_double(s->freeram));
81
82         json_object_object_add(obj, "shared",
83                                json_object_new_double(s->sharedram));
84
85         json_object_object_add(obj, "buffer",
86                                json_object_new_double(s->bufferram));
87
88         return obj;
89 }
90
91 static json_object *swap_to_json_object(const struct sysinfo *s)
92 {
93         json_object *obj = json_object_new_object();
94
95         json_object_object_add(obj, "total",
96                                json_object_new_double(s->totalswap));
97
98         json_object_object_add(obj, "free",
99                                json_object_new_double(s->freeswap));
100
101         return obj;
102 }
103
104 static json_object *netif_to_json_object(const char *netif)
105 {
106         glibtop_netload buf;
107         json_object *obj = json_object_new_object();
108
109         json_object_object_add(obj, "name", json_object_new_string(netif));
110
111         glibtop_get_netload(&buf, netif);
112
113         json_object_object_add(obj, "bytes_in",
114                                json_object_new_double(buf.bytes_in));
115
116         json_object_object_add(obj, "bytes_out",
117                                json_object_new_double(buf.bytes_out));
118
119         return obj;
120 }
121
122 static json_object *net_to_json_object(const struct psysinfo *s)
123 {
124         char **netif = s->interfaces;
125         json_object *net = json_object_new_array();
126
127         while (*netif) {
128                 json_object_array_add(net, netif_to_json_object(*netif));
129
130                 netif++;
131         }
132
133         return net;
134 }
135
136 static json_object *sysinfo_to_json_object(const struct psysinfo *s)
137 {
138         static float load_scale = 1 << SI_LOAD_SHIFT;
139         json_object *mo;
140         json_object *obj = json_object_new_object();
141         struct measure *m;
142
143         json_object_object_add(obj, "load",
144                                json_object_new_double(s->cpu_rate));
145
146         json_object_object_add
147                 (obj, "load_1",
148                  json_object_new_double(s->sysinfo.loads[0] / load_scale));
149
150         json_object_object_add
151                 (obj, "load_5",
152                  json_object_new_double(s->sysinfo.loads[1] / load_scale));
153
154         json_object_object_add
155                 (obj, "load_15",
156                  json_object_new_double(s->sysinfo.loads[2] / load_scale));
157
158         json_object_object_add
159                 (obj, "uptime", json_object_new_double(s->sysinfo.uptime));
160
161         json_object_object_add
162                 (obj, "mem_unit", json_object_new_double(s->sysinfo.mem_unit));
163
164         json_object_object_add(obj, "ram", ram_to_json_object(&s->sysinfo));
165         json_object_object_add(obj, "swap", swap_to_json_object(&s->sysinfo));
166         json_object_object_add(obj, "net", net_to_json_object(s));
167
168         return obj;
169 }
170
171 char *sysinfo_to_json_string(const struct psysinfo *s)
172 {
173         char *str;
174         json_object *obj = sysinfo_to_json_object(s);
175
176         str = strdup(json_object_to_json_string(obj));
177
178         json_object_put(obj);
179
180         return str;
181 }