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