Imported Upstream version 0.6.2.17
[psensor-pkg-debian.git] / src / server / sysinfo.c
1 /*
2  * Copyright (C) 2010-2012 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
24 #include <json/json.h>
25
26 #include "sysinfo.h"
27
28 static glibtop_cpu *cpu;
29 static float last_used;
30 static float last_total;
31
32 void sysinfo_update(struct psysinfo *info)
33 {
34         unsigned long int used = 0;
35         unsigned long int dt;
36         glibtop_netlist buf;
37
38         /* cpu */
39         if (!cpu)
40                 cpu = malloc(sizeof(glibtop_cpu));
41
42         glibtop_get_cpu(cpu);
43
44         used = cpu->user + cpu->nice + cpu->sys;
45
46         dt = cpu->total - last_total;
47
48         if (dt)
49                 info->cpu_rate = (used - last_used) / dt;
50
51         last_used = used;
52         last_total = cpu->total;
53
54         glibtop_get_loadavg(&info->loadavg);
55         glibtop_get_mem(&info->mem);
56         glibtop_get_swap(&info->swap);
57         glibtop_get_uptime(&info->uptime);
58
59         /* network */
60         if (!info->interfaces)
61                 info->interfaces = glibtop_get_netlist(&buf);
62 }
63
64 void sysinfo_cleanup()
65 {
66         if (cpu)
67                 free(cpu);
68 }
69
70 static json_object *ram_to_json_object(const struct psysinfo *s)
71 {
72         json_object *obj = json_object_new_object();
73
74         json_object_object_add(obj, "total",
75                                json_object_new_double(s->mem.total));
76
77         json_object_object_add(obj, "free",
78                                json_object_new_double(s->mem.free));
79
80         json_object_object_add(obj, "shared",
81                                json_object_new_double(s->mem.shared));
82
83         json_object_object_add(obj, "buffer",
84                                json_object_new_double(s->mem.buffer));
85
86         return obj;
87 }
88
89 static json_object *swap_to_json_object(const struct psysinfo *s)
90 {
91         json_object *obj = json_object_new_object();
92
93         json_object_object_add(obj, "total",
94                                json_object_new_double(s->swap.total));
95
96         json_object_object_add(obj, "free",
97                                json_object_new_double(s->swap.free));
98
99         return obj;
100 }
101
102 static json_object *netif_to_json_object(const char *netif)
103 {
104         glibtop_netload buf;
105         json_object *obj = json_object_new_object();
106
107         json_object_object_add(obj, "name", json_object_new_string(netif));
108
109         glibtop_get_netload(&buf, netif);
110
111         json_object_object_add(obj, "bytes_in",
112                                json_object_new_double(buf.bytes_in));
113
114         json_object_object_add(obj, "bytes_out",
115                                json_object_new_double(buf.bytes_out));
116
117         return obj;
118 }
119
120 static json_object *net_to_json_object(const struct psysinfo *s)
121 {
122         char **netif = s->interfaces;
123         json_object *net = json_object_new_array();
124
125         while (*netif) {
126                 json_object_array_add(net, netif_to_json_object(*netif));
127
128                 netif++;
129         }
130
131         return net;
132 }
133
134 static json_object *sysinfo_to_json_object(const struct psysinfo *s)
135 {
136         json_object *obj;
137
138         obj = json_object_new_object();
139
140         json_object_object_add(obj, "load",
141                                json_object_new_double(s->cpu_rate));
142
143         json_object_object_add
144                 (obj, "load_1",
145                  json_object_new_double(s->loadavg.loadavg[0]));
146
147         json_object_object_add
148                 (obj, "load_5",
149                  json_object_new_double(s->loadavg.loadavg[1]));
150
151         json_object_object_add
152                 (obj, "load_15",
153                  json_object_new_double(s->loadavg.loadavg[2]));
154
155         json_object_object_add
156                 (obj, "uptime", json_object_new_double(s->uptime.uptime));
157
158         json_object_object_add
159                 (obj, "mem_unit", json_object_new_double(1));
160
161         json_object_object_add(obj, "ram", ram_to_json_object(s));
162         json_object_object_add(obj, "swap", swap_to_json_object(s));
163         json_object_object_add(obj, "net", net_to_json_object(s));
164
165         return obj;
166 }
167
168 char *sysinfo_to_json_string(const struct psysinfo *s)
169 {
170         char *str;
171         json_object *obj = sysinfo_to_json_object(s);
172
173         str = strdup(json_object_to_json_string(obj));
174
175         json_object_put(obj);
176
177         return str;
178 }