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 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 measures = data["measures"];
106 $("h1").append(data["name"]);
107 $("title").append(data["name"]);
109 $.each(measures, function(i, item) {
110 value = item["value"];
111 date = new Date(item["time"]*1000);
112 entry = [date, item["value"]];
114 data_chart.push(entry);
116 if (!max_date || max_date < date)
118 if (!min_date || min_date > date)
121 if (!min || value < min)
123 if (!max || value > max)
131 renderer: $.jqplot.DateAxisRenderer,
133 formatString:'%H:%M:%S'
149 $.jqplot (chart_id, [data_chart], style);
152 function update_menu() {
153 var name, link, url, str;
157 $.getJSON("/api/1.0/sensors", function(data) {
158 str += "<li><em>Sensors</em>\n<ul>";
160 $.each(data, function(i, item) {
162 url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
163 link = "<a href='"+url+"'>"+name+"</a>";
164 str += "<li>"+link+"</li>";
168 str += "<li><em>CPU</em><ul>";
169 url = "details.html?id="+escape("/api/1.0/cpu/usage");
170 link = "<a href='"+url+"'>usage</a>";
171 str += "<li>"+link+"</li>";
175 $("#menu-list").append(str);
181 function update_summary_sensors() {
182 var name, value_str, min_str, max_str, type, type_str, url;
184 $.getJSON("/api/1.0/sensors", function(data) {
185 $("#sensors tbody").html("");
187 $.each(data, function(i, item) {
190 value_str = value_to_str(item["last_measure"]["value"], type);
191 min_str = value_to_str(item["min"], type);
192 max_str = value_to_str(item["max"], type);
193 type_str = type_to_str(type);
194 url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
196 $("#sensors tbody").append("<tr>"
197 +"<td><a href='"+url+"'>"+name+"</a></td>"
198 +"<td>"+value_str+"</td>"
199 +"<td>"+min_str+"</td>"
200 +"<td>"+max_str+"</td>"
201 +"<td>"+type_str+"</td>"
207 function update_summary_sysinfo() {
208 $.getJSON("/api/1.0/sysinfo", function(data) {
209 $("#uptime").html("");
210 $("#cpu tbody").html("");
211 $("#memory").html("");
213 $("#net tbody").html("");
215 var load = Math.round(data["load"] * 100);
216 var load_1 = Math.round(data["load_1"]*1000)/1000;
217 var load_5 = Math.round(data["load_5"]*1000)/1000;
218 var load_15 = Math.round(data["load_15"]*1000)/1000;
219 var uptime = data["uptime"];
220 var uptime_s = uptime % 60;
221 var uptime_mn = Math.floor((uptime / 60) % 60);
222 var uptime_h = Math.floor((uptime / (60*60)) % 24);
223 var uptime_d = Math.floor(uptime / (60*60*24));
225 $("#cpu").append("<tr><td><a href='details.html?id=/api/1.0/cpu/usage'>"+load+"%</a></td><td>"
228 +load_15+"</td></tr>");
230 $("#uptime").append(uptime_d+"d "+uptime_h+"h "+uptime_mn+"mn");
232 var ram = data["ram"];
233 var swap = data["swap"];
234 var mu = data["mem_unit"];
236 var ramtotal = ram["total"]*mu;
237 var ramfree = ram["free"]*mu;
238 var ramused = (ram["total"] - ram["free"])*mu;
239 var ramshared = ram["shared"]*mu;
240 var rambuffer = ram["buffer"]*mu;
243 $("#memory").append("<td>Memory</td>"
244 +"<td>"+format_mem_size(ramtotal)+"</td>"
245 +"<td>"+format_mem_size(ramused)+"</td>"
246 +"<td>"+format_mem_size(ramfree)+"</td>"
247 +"<td>"+format_mem_size(ramshared)+"</td>"
248 +"<td>"+format_mem_size(rambuffer)+"</td>");
250 $("#swap").append("<td>Swap</td>"
251 +"<td>"+format_mem_size(swap["total"]*mu)+"</td>"
252 +"<td>"+format_mem_size(swap["total"]*mu-swap["free"]*mu)+"</td>"
253 +"<td>"+format_mem_size(swap["free"]*mu)+"</td>");
255 var netdata = data["net"];
256 $.each(netdata, function(i, item) {
257 $("#net").append("<tr><td>"+item["name"]+"</td>"
258 +"<td>"+format_mem_size(item["bytes_in"])+"</td>"
259 +"<td>"+format_mem_size(item["bytes_out"])+"</td></tr>");