2 * Copyright (C) 2010-2011 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>
23 #include <sys/sysinfo.h>
25 #include <json/json.h>
29 static glibtop_cpu *cpu;
30 static float last_used;
31 static float last_total;
33 void sysinfo_update(struct psysinfo *info)
35 unsigned long int used = 0;
41 cpu = malloc(sizeof(glibtop_cpu));
45 used = cpu->user + cpu->nice + cpu->sys;
47 dt = cpu->total - last_total;
50 info->cpu_rate = (used - last_used) / dt;
53 last_total = cpu->total;
56 sysinfo(&info->sysinfo);
59 if (!info->interfaces)
60 info->interfaces = glibtop_get_netlist(&buf);
63 void sysinfo_cleanup()
69 static json_object *ram_to_json_object(const struct sysinfo *s)
71 json_object *obj = json_object_new_object();
73 json_object_object_add(obj, "total",
74 json_object_new_double(s->totalram));
76 json_object_object_add(obj, "free",
77 json_object_new_double(s->freeram));
79 json_object_object_add(obj, "shared",
80 json_object_new_double(s->sharedram));
82 json_object_object_add(obj, "buffer",
83 json_object_new_double(s->bufferram));
88 static json_object *swap_to_json_object(const struct sysinfo *s)
90 json_object *obj = json_object_new_object();
92 json_object_object_add(obj, "total",
93 json_object_new_double(s->totalswap));
95 json_object_object_add(obj, "free",
96 json_object_new_double(s->freeswap));
101 static json_object *netif_to_json_object(const char *netif)
104 json_object *obj = json_object_new_object();
106 json_object_object_add(obj, "name", json_object_new_string(netif));
108 glibtop_get_netload(&buf, netif);
110 json_object_object_add(obj, "bytes_in",
111 json_object_new_double(buf.bytes_in));
113 json_object_object_add(obj, "bytes_out",
114 json_object_new_double(buf.bytes_out));
119 static json_object *net_to_json_object(const struct psysinfo *s)
121 char **netif = s->interfaces;
122 json_object *net = json_object_new_array();
125 json_object_array_add(net, netif_to_json_object(*netif));
133 static json_object *sysinfo_to_json_object(const struct psysinfo *s)
135 static float load_scale = 1 << SI_LOAD_SHIFT;
136 json_object *obj = json_object_new_object();
138 json_object_object_add(obj, "load",
139 json_object_new_double(s->cpu_rate));
141 json_object_object_add
143 json_object_new_double(s->sysinfo.loads[0] / load_scale));
145 json_object_object_add
147 json_object_new_double(s->sysinfo.loads[1] / load_scale));
149 json_object_object_add
151 json_object_new_double(s->sysinfo.loads[2] / load_scale));
153 json_object_object_add
154 (obj, "uptime", json_object_new_double(s->sysinfo.uptime));
156 json_object_object_add
157 (obj, "mem_unit", json_object_new_double(s->sysinfo.mem_unit));
159 json_object_object_add(obj, "ram", ram_to_json_object(&s->sysinfo));
160 json_object_object_add(obj, "swap", swap_to_json_object(&s->sysinfo));
161 json_object_object_add(obj, "net", net_to_json_object(s));
166 char *sysinfo_to_json_string(const struct psysinfo *s)
169 json_object *obj = sysinfo_to_json_object(s);
171 str = strdup(json_object_to_json_string(obj));
173 json_object_put(obj);