import psensor trunk from private svn
[psensor.git] / src / server / server_lua.c
1 /*
2     Copyright (C) 2010-2011 wpitchoune@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
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
17     02110-1301 USA
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "server_lua.h"
24
25 #include "plib/plib_luatpl.h"
26
27 int init_lua(lua_State *L, void *data)
28 {
29         struct server_data *server_data = data;
30         struct psensor **s_cur;
31         struct psensor **sensors = server_data->sensors;
32         int i;
33         static float load_scale = 1 << SI_LOAD_SHIFT;
34
35         lua_newtable(L);
36
37 #ifdef HAVE_GTOP
38         lua_pushstring(L, "load");
39         lua_pushnumber(L, server_data->psysinfo.cpu_rate);
40         lua_settable(L, -3);
41 #endif
42
43         lua_pushstring(L, "uptime");
44         lua_pushnumber(L, server_data->psysinfo.sysinfo.uptime);
45         lua_settable(L, -3);
46
47         lua_pushstring(L, "load_1mn");
48         lua_pushnumber(L, server_data->psysinfo.sysinfo.loads[0] / load_scale);
49         lua_settable(L, -3);
50
51         lua_pushstring(L, "load_5mn");
52         lua_pushnumber(L, server_data->psysinfo.sysinfo.loads[1] / load_scale);
53         lua_settable(L, -3);
54
55         lua_pushstring(L, "load_15mn");
56         lua_pushnumber(L, server_data->psysinfo.sysinfo.loads[2] / load_scale);
57         lua_settable(L, -3);
58
59         lua_pushstring(L, "freeram");
60         lua_pushnumber(L, server_data->psysinfo.sysinfo.freeram);
61         lua_settable(L, -3);
62
63         lua_pushstring(L, "sharedram");
64         lua_pushnumber(L, server_data->psysinfo.sysinfo.sharedram);
65         lua_settable(L, -3);
66
67         lua_pushstring(L, "bufferram");
68         lua_pushnumber(L, server_data->psysinfo.sysinfo.bufferram);
69         lua_settable(L, -3);
70
71         lua_pushstring(L, "totalswap");
72         lua_pushnumber(L, server_data->psysinfo.sysinfo.totalswap);
73         lua_settable(L, -3);
74
75         lua_pushstring(L, "freeswap");
76         lua_pushnumber(L, server_data->psysinfo.sysinfo.freeswap);
77         lua_settable(L, -3);
78
79         lua_pushstring(L, "procs");
80         lua_pushnumber(L, server_data->psysinfo.sysinfo.procs);
81         lua_settable(L, -3);
82
83         lua_pushstring(L, "totalhigh");
84         lua_pushnumber(L, server_data->psysinfo.sysinfo.totalhigh);
85         lua_settable(L, -3);
86
87         lua_pushstring(L, "freehigh");
88         lua_pushnumber(L, server_data->psysinfo.sysinfo.freehigh);
89         lua_settable(L, -3);
90
91         lua_pushstring(L, "totalram");
92         lua_pushnumber(L, server_data->psysinfo.sysinfo.totalram);
93         lua_settable(L, -3);
94
95         lua_pushstring(L, "mem_unit");
96         lua_pushnumber(L, server_data->psysinfo.sysinfo.mem_unit);
97         lua_settable(L, -3);
98
99
100         lua_setglobal(L, "sysinfo");
101
102
103         lua_newtable(L);
104
105         s_cur = sensors;
106         i = 1;
107         while (*s_cur) {
108                 lua_pushnumber(L, i);
109
110                 lua_newtable(L);
111
112                 lua_pushstring(L, "name");
113                 lua_pushstring(L, (*s_cur)->name);
114                 lua_settable(L, -3);
115
116                 lua_pushstring(L, "measure_last");
117                 lua_pushnumber(L, psensor_get_current_value(*s_cur));
118                 lua_settable(L, -3);
119
120                 lua_pushstring(L, "measure_min");
121                 lua_pushnumber(L, (*s_cur)->min);
122                 lua_settable(L, -3);
123
124                 lua_pushstring(L, "measure_max");
125                 lua_pushnumber(L, (*s_cur)->max);
126                 lua_settable(L, -3);
127
128                 lua_settable(L, -3);
129
130                 s_cur++;
131                 i++;
132         }
133
134         lua_setglobal(L, "sensors");
135
136         lua_pushstring(L, VERSION);
137         lua_setglobal(L, "psensor_version");
138
139         return 1;
140 }
141
142 char *lua_to_html_page(struct server_data *server_data, const char *fpath)
143 {
144         char *page = NULL;
145         struct luatpl_error err;
146
147         err.message = NULL;
148
149         page = luatpl_generate(fpath,
150                                init_lua,
151                                server_data,
152                                &err);
153
154         if (!page) {
155                 luatpl_fprint_error(stderr,
156                                     &err,
157                                     fpath,
158                                     "outstring");
159                 free(err.message);
160         }
161
162         return page;
163 }