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;
42 cpu = malloc(sizeof(glibtop_cpu));
46 used = cpu->user + cpu->nice + cpu->sys;
48 dt = cpu->total - last_total;
51 info->cpu_rate = (used - last_used) / dt;
54 last_total = cpu->total;
57 sysinfo(&info->sysinfo);
60 if (!info->interfaces)
61 info->interfaces = glibtop_get_netlist(&buf);
64 void sysinfo_cleanup()
70 static json_object *ram_to_json_object(const struct sysinfo *s)
72 json_object *obj = json_object_new_object();
74 json_object_object_add(obj, "total",
75 json_object_new_double(s->totalram));
77 json_object_object_add(obj, "free",
78 json_object_new_double(s->freeram));
80 json_object_object_add(obj, "shared",
81 json_object_new_double(s->sharedram));
83 json_object_object_add(obj, "buffer",
84 json_object_new_double(s->bufferram));
89 static json_object *swap_to_json_object(const struct sysinfo *s)
91 json_object *obj = json_object_new_object();
93 json_object_object_add(obj, "total",
94 json_object_new_double(s->totalswap));
96 json_object_object_add(obj, "free",
97 json_object_new_double(s->freeswap));
102 static json_object *netif_to_json_object(const char *netif)
105 json_object *obj = json_object_new_object();
107 json_object_object_add(obj, "name", json_object_new_string(netif));
109 glibtop_get_netload(&buf, netif);
111 json_object_object_add(obj, "bytes_in",
112 json_object_new_double(buf.bytes_in));
114 json_object_object_add(obj, "bytes_out",
115 json_object_new_double(buf.bytes_out));
120 static json_object *net_to_json_object(const struct psysinfo *s)
122 char **netif = s->interfaces;
123 json_object *net = json_object_new_array();
126 json_object_array_add(net, netif_to_json_object(*netif));
134 static json_object *sysinfo_to_json_object(const struct psysinfo *s)
136 static float load_scale = 1 << SI_LOAD_SHIFT;
137 json_object *obj = json_object_new_object();
139 json_object_object_add(obj, "load",
140 json_object_new_double(s->cpu_rate));
142 json_object_object_add
144 json_object_new_double(s->sysinfo.loads[0] / load_scale));
146 json_object_object_add
148 json_object_new_double(s->sysinfo.loads[1] / load_scale));
150 json_object_object_add
152 json_object_new_double(s->sysinfo.loads[2] / load_scale));
154 json_object_object_add
155 (obj, "uptime", json_object_new_double(s->sysinfo.uptime));
157 json_object_object_add
158 (obj, "mem_unit", json_object_new_double(s->sysinfo.mem_unit));
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));
167 char *sysinfo_to_json_string(const struct psysinfo *s)
170 json_object *obj = sysinfo_to_json_object(s);
172 str = strdup(json_object_to_json_string(obj));
174 json_object_put(obj);