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