2 * Copyright (C) 2010-2012 jeanfi@gmail.com
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.
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.
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
20 #include <glibtop/cpu.h>
21 #include <glibtop/netlist.h>
22 #include <glibtop/netload.h>
24 #include <json/json.h>
28 static glibtop_cpu *cpu;
29 static float last_used;
30 static float last_total;
32 void sysinfo_update(struct psysinfo *info)
34 unsigned long int used = 0;
40 cpu = malloc(sizeof(glibtop_cpu));
44 used = cpu->user + cpu->nice + cpu->sys;
46 dt = cpu->total - last_total;
49 info->cpu_rate = (used - last_used) / dt;
52 last_total = cpu->total;
54 glibtop_get_loadavg(&info->loadavg);
55 glibtop_get_mem(&info->mem);
56 glibtop_get_swap(&info->swap);
57 glibtop_get_uptime(&info->uptime);
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 psysinfo *s)
72 json_object *obj = json_object_new_object();
74 json_object_object_add(obj, "total",
75 json_object_new_double(s->mem.total));
77 json_object_object_add(obj, "free",
78 json_object_new_double(s->mem.free));
80 json_object_object_add(obj, "shared",
81 json_object_new_double(s->mem.shared));
83 json_object_object_add(obj, "buffer",
84 json_object_new_double(s->mem.buffer));
89 static json_object *swap_to_json_object(const struct psysinfo *s)
91 json_object *obj = json_object_new_object();
93 json_object_object_add(obj, "total",
94 json_object_new_double(s->swap.total));
96 json_object_object_add(obj, "free",
97 json_object_new_double(s->swap.free));
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)
138 obj = json_object_new_object();
140 json_object_object_add(obj, "load",
141 json_object_new_double(s->cpu_rate));
143 json_object_object_add
145 json_object_new_double(s->loadavg.loadavg[0]));
147 json_object_object_add
149 json_object_new_double(s->loadavg.loadavg[1]));
151 json_object_object_add
153 json_object_new_double(s->loadavg.loadavg[2]));
155 json_object_object_add
156 (obj, "uptime", json_object_new_double(s->uptime.uptime));
158 json_object_object_add
159 (obj, "mem_unit", json_object_new_double(1));
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));
168 char *sysinfo_to_json_string(const struct psysinfo *s)
171 json_object *obj = sysinfo_to_json_object(s);
173 str = strdup(json_object_to_json_string(obj));
175 json_object_put(obj);