2 * Copyright (C) 2010-2014 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>
27 #include <json/json.h>
29 #include <json-c/json.h>
34 static glibtop_cpu *cpu;
35 static float last_used;
36 static float last_total;
38 void sysinfo_update(struct psysinfo *info)
40 unsigned long int used = 0;
46 cpu = malloc(sizeof(glibtop_cpu));
50 used = cpu->user + cpu->nice + cpu->sys;
52 dt = cpu->total - last_total;
55 info->cpu_rate = (used - last_used) / dt;
58 last_total = cpu->total;
60 glibtop_get_loadavg(&info->loadavg);
61 glibtop_get_mem(&info->mem);
62 glibtop_get_swap(&info->swap);
63 glibtop_get_uptime(&info->uptime);
66 if (!info->interfaces)
67 info->interfaces = glibtop_get_netlist(&buf);
70 void sysinfo_cleanup()
76 static json_object *ram_to_json_object(const struct psysinfo *s)
78 json_object *obj = json_object_new_object();
80 json_object_object_add(obj, "total",
81 json_object_new_double(s->mem.total));
83 json_object_object_add(obj, "free",
84 json_object_new_double(s->mem.free));
86 json_object_object_add(obj, "shared",
87 json_object_new_double(s->mem.shared));
89 json_object_object_add(obj, "buffer",
90 json_object_new_double(s->mem.buffer));
95 static json_object *swap_to_json_object(const struct psysinfo *s)
97 json_object *obj = json_object_new_object();
99 json_object_object_add(obj, "total",
100 json_object_new_double(s->swap.total));
102 json_object_object_add(obj, "free",
103 json_object_new_double(s->swap.free));
108 static json_object *netif_to_json_object(const char *netif)
111 json_object *obj = json_object_new_object();
113 json_object_object_add(obj, "name", json_object_new_string(netif));
115 glibtop_get_netload(&buf, netif);
117 json_object_object_add(obj, "bytes_in",
118 json_object_new_double(buf.bytes_in));
120 json_object_object_add(obj, "bytes_out",
121 json_object_new_double(buf.bytes_out));
126 static json_object *net_to_json_object(const struct psysinfo *s)
128 char **netif = s->interfaces;
129 json_object *net = json_object_new_array();
132 json_object_array_add(net, netif_to_json_object(*netif));
140 static json_object *sysinfo_to_json_object(const struct psysinfo *s)
144 obj = json_object_new_object();
146 json_object_object_add(obj, "load",
147 json_object_new_double(s->cpu_rate));
149 json_object_object_add
151 json_object_new_double(s->loadavg.loadavg[0]));
153 json_object_object_add
155 json_object_new_double(s->loadavg.loadavg[1]));
157 json_object_object_add
159 json_object_new_double(s->loadavg.loadavg[2]));
161 json_object_object_add
162 (obj, "uptime", json_object_new_double(s->uptime.uptime));
164 json_object_object_add
165 (obj, "mem_unit", json_object_new_double(1));
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));
174 char *sysinfo_to_json_string(const struct psysinfo *s)
177 json_object *obj = sysinfo_to_json_object(s);
179 str = strdup(json_object_to_json_string(obj));
181 json_object_put(obj);