removed unused local vars
[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
40         /* cpu */
41         if (!cpu)
42                 cpu = malloc(sizeof(glibtop_cpu));
43
44         glibtop_get_cpu(cpu);
45
46         used = cpu->user + cpu->nice + cpu->sys;
47
48         dt = cpu->total - last_total;
49
50         if (dt)
51                 info->cpu_rate = (used - last_used) / dt;
52
53         last_used = used;
54         last_total = cpu->total;
55
56         /* memory */
57         sysinfo(&info->sysinfo);
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 sysinfo *s)
71 {
72         json_object *obj = json_object_new_object();
73
74         json_object_object_add(obj, "total",
75                                json_object_new_double(s->totalram));
76
77         json_object_object_add(obj, "free",
78                                json_object_new_double(s->freeram));
79
80         json_object_object_add(obj, "shared",
81                                json_object_new_double(s->sharedram));
82
83         json_object_object_add(obj, "buffer",
84                                json_object_new_double(s->bufferram));
85
86         return obj;
87 }
88
89 static json_object *swap_to_json_object(const struct sysinfo *s)
90 {
91         json_object *obj = json_object_new_object();
92
93         json_object_object_add(obj, "total",
94                                json_object_new_double(s->totalswap));
95
96         json_object_object_add(obj, "free",
97                                json_object_new_double(s->freeswap));
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         static float load_scale = 1 << SI_LOAD_SHIFT;
137         json_object *obj = json_object_new_object();
138
139         json_object_object_add(obj, "load",
140                                json_object_new_double(s->cpu_rate));
141
142         json_object_object_add
143                 (obj, "load_1",
144                  json_object_new_double(s->sysinfo.loads[0] / load_scale));
145
146         json_object_object_add
147                 (obj, "load_5",
148                  json_object_new_double(s->sysinfo.loads[1] / load_scale));
149
150         json_object_object_add
151                 (obj, "load_15",
152                  json_object_new_double(s->sysinfo.loads[2] / load_scale));
153
154         json_object_object_add
155                 (obj, "uptime", json_object_new_double(s->sysinfo.uptime));
156
157         json_object_object_add
158                 (obj, "mem_unit", json_object_new_double(s->sysinfo.mem_unit));
159
160         json_object_object_add(obj, "ram", ram_to_json_object(&s->sysinfo));
161         json_object_object_add(obj, "swap", swap_to_json_object(&s->sysinfo));
162         json_object_object_add(obj, "net", net_to_json_object(s));
163
164         return obj;
165 }
166
167 char *sysinfo_to_json_string(const struct psysinfo *s)
168 {
169         char *str;
170         json_object *obj = sysinfo_to_json_object(s);
171
172         str = strdup(json_object_to_json_string(obj));
173
174         json_object_put(obj);
175
176         return str;
177 }