2 Copyright (C) 2010-2011 jeanfi@gmail.com
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.
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.
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
21 #include <glibtop/cpu.h>
22 #include <glibtop/netlist.h>
23 #include <glibtop/netload.h>
24 #include <sys/sysinfo.h>
26 #include <json/json.h>
30 static glibtop_cpu *cpu;
31 static float last_used;
32 static float last_total;
34 void sysinfo_update(struct psysinfo *info)
36 unsigned long int used = 0;
44 cpu = malloc(sizeof(glibtop_cpu));
48 used = cpu->user + cpu->nice + cpu->sys;
50 dt = cpu->total - last_total;
53 info->cpu_rate = (used - last_used) / dt;
56 last_total = cpu->total;
59 sysinfo(&info->sysinfo);
62 if (!info->interfaces)
63 info->interfaces = glibtop_get_netlist(&buf);
66 void sysinfo_cleanup()
72 static json_object *ram_to_json_object(const struct sysinfo *s)
74 json_object *obj = json_object_new_object();
76 json_object_object_add(obj, "total",
77 json_object_new_double(s->totalram));
79 json_object_object_add(obj, "free",
80 json_object_new_double(s->freeram));
82 json_object_object_add(obj, "shared",
83 json_object_new_double(s->sharedram));
85 json_object_object_add(obj, "buffer",
86 json_object_new_double(s->bufferram));
91 static json_object *swap_to_json_object(const struct sysinfo *s)
93 json_object *obj = json_object_new_object();
95 json_object_object_add(obj, "total",
96 json_object_new_double(s->totalswap));
98 json_object_object_add(obj, "free",
99 json_object_new_double(s->freeswap));
104 static json_object *netif_to_json_object(const char *netif)
107 json_object *obj = json_object_new_object();
109 json_object_object_add(obj, "name", json_object_new_string(netif));
111 glibtop_get_netload(&buf, netif);
113 json_object_object_add(obj, "bytes_in",
114 json_object_new_double(buf.bytes_in));
116 json_object_object_add(obj, "bytes_out",
117 json_object_new_double(buf.bytes_out));
122 static json_object *net_to_json_object(const struct psysinfo *s)
124 char **netif = s->interfaces;
125 json_object *net = json_object_new_array();
128 json_object_array_add(net, netif_to_json_object(*netif));
136 static json_object *sysinfo_to_json_object(const struct psysinfo *s)
138 static float load_scale = 1 << SI_LOAD_SHIFT;
140 json_object *obj = json_object_new_object();
143 json_object_object_add(obj, "load",
144 json_object_new_double(s->cpu_rate));
146 json_object_object_add
148 json_object_new_double(s->sysinfo.loads[0] / load_scale));
150 json_object_object_add
152 json_object_new_double(s->sysinfo.loads[1] / load_scale));
154 json_object_object_add
156 json_object_new_double(s->sysinfo.loads[2] / load_scale));
158 json_object_object_add
159 (obj, "uptime", json_object_new_double(s->sysinfo.uptime));
161 json_object_object_add
162 (obj, "mem_unit", json_object_new_double(s->sysinfo.mem_unit));
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));
171 char *sysinfo_to_json_string(const struct psysinfo *s)
174 json_object *obj = sysinfo_to_json_object(s);
176 str = strdup(json_object_to_json_string(obj));
178 json_object_put(obj);