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 function format_mem_size(s) {
21 var mo_bytes, go_bytes, o, k, m, g;
23 mo_bytes = 1024 * 1024;
24 go_bytes = 1024 * mo_bytes;
27 k = Math.round((s / 1024) % 1024);
28 m = Math.round((s / (1024*1024)) % 1024);
29 g = Math.round(s / (1024*1024*1024));
46 function type_to_str(stype) {
53 else if (stype & 0x0200)
55 else if (stype & 0x0400)
57 else if (stype & 0x0800)
58 stype_str = "CPU Usage Percentage";
59 else if (stype & 0x1000)
63 stype_str += " Temperature";
64 else if (stype & 0x0002)
70 function type_to_unit(stype) {
73 else if (stype & 0x0002)
79 function value_to_str(value, type) {
80 return value+type_to_unit(type);
83 function get_url_params() {
87 hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
89 for(i = 0; i < hashes.length; i++) {
90 hash = hashes[i].split('=');
92 vars[hash[0]] = hash[1];
98 function update_chart(chart_id, title, data) {
99 var min_date, max_date, min, max, value;
100 var measures, data_chart, date, entry;
103 $("#"+chart_id).html("");
105 measures = data["measures"];
109 $("h1").append(data["name"]);
112 $("title").html(data["name"]);
114 // IE8 doesn't allow to modify the page title
117 $.each(measures, function(i, item) {
118 value = item["value"];
119 date = new Date(item["time"]*1000);
120 entry = [date, item["value"]];
122 data_chart.push(entry);
124 if (!max_date || max_date < date)
126 if (!min_date || min_date > date)
129 if (!min || value < min)
131 if (!max || value > max)
139 renderer: $.jqplot.DateAxisRenderer,
141 formatString:'%H:%M:%S'
158 $.jqplot (chart_id, [data_chart], style);
161 function update_menu() {
162 var name, link, url, str;
166 $.getJSON("/api/1.0/sensors", function(data) {
167 str += "<li><em>Sensors</em>\n<ul>";
169 $.each(data, function(i, item) {
171 url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
172 link = "<a href='"+url+"'>"+name+"</a>";
173 str += "<li>"+link+"</li>";
177 str += "<li><em>CPU</em><ul>";
178 url = "details.html?id="+escape("/api/1.0/cpu/usage");
179 link = "<a href='"+url+"'>usage</a>";
180 str += "<li>"+link+"</li>";
184 $("#menu-list").append(str);
190 function update_summary_sensors() {
191 var name, value_str, min_str, max_str, type, type_str, url;
193 $.getJSON("/api/1.0/sensors", function(data) {
194 $("#sensors tbody").html("");
196 $.each(data, function(i, item) {
199 value_str = value_to_str(item["last_measure"]["value"], type);
200 min_str = value_to_str(item["min"], type);
201 max_str = value_to_str(item["max"], type);
202 type_str = type_to_str(type);
203 url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
205 $("#sensors tbody").append("<tr>"
206 +"<td><a href='"+url+"'>"+name+"</a></td>"
207 +"<td>"+value_str+"</td>"
208 +"<td>"+min_str+"</td>"
209 +"<td>"+max_str+"</td>"
210 +"<td>"+type_str+"</td>"
216 function update_summary_sysinfo() {
217 $.getJSON("/api/1.0/sysinfo", function(data) {
218 $("#uptime").html("");
219 $("#cpu tbody").html("");
220 $("#memory").html("");
222 $("#net tbody").html("");
224 var load = Math.round(data["load"] * 100);
225 var load_1 = Math.round(data["load_1"]*1000)/1000;
226 var load_5 = Math.round(data["load_5"]*1000)/1000;
227 var load_15 = Math.round(data["load_15"]*1000)/1000;
228 var uptime = data["uptime"];
229 var uptime_s = uptime % 60;
230 var uptime_mn = Math.floor((uptime / 60) % 60);
231 var uptime_h = Math.floor((uptime / (60*60)) % 24);
232 var uptime_d = Math.floor(uptime / (60*60*24));
234 $("#cpu").append("<tr><td><a href='details.html?id=/api/1.0/cpu/usage'>"+load+"%</a></td><td>"
237 +load_15+"</td></tr>");
239 $("#uptime").append(uptime_d+"d "+uptime_h+"h "+uptime_mn+"mn");
241 var ram = data["ram"];
242 var swap = data["swap"];
243 var mu = data["mem_unit"];
245 var ramtotal = ram["total"]*mu;
246 var ramfree = ram["free"]*mu;
247 var ramused = (ram["total"] - ram["free"])*mu;
248 var ramshared = ram["shared"]*mu;
249 var rambuffer = ram["buffer"]*mu;
252 $("#memory").append("<td>Memory</td>"
253 +"<td>"+format_mem_size(ramtotal)+"</td>"
254 +"<td>"+format_mem_size(ramused)+"</td>"
255 +"<td>"+format_mem_size(ramfree)+"</td>"
256 +"<td>"+format_mem_size(ramshared)+"</td>"
257 +"<td>"+format_mem_size(rambuffer)+"</td>");
259 $("#swap").append("<td>Swap</td>"
260 +"<td>"+format_mem_size(swap["total"]*mu)+"</td>"
261 +"<td>"+format_mem_size(swap["total"]*mu-swap["free"]*mu)+"</td>"
262 +"<td>"+format_mem_size(swap["free"]*mu)+"</td>");
264 var netdata = data["net"];
265 $.each(netdata, function(i, item) {
266 $("#net").append("<tr><td>"+item["name"]+"</td>"
267 +"<td>"+format_mem_size(item["bytes_in"])+"</td>"
268 +"<td>"+format_mem_size(item["bytes_out"])+"</td></tr>");