2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include <sys/sysinfo.h>
24 #include <sys/utsname.h>
28 static struct blob_buf buf;
31 rpc_system_board(struct ubus_context *ctx, struct ubus_object *obj,
32 struct ubus_request_data *req, const char *method,
33 struct blob_attr *msg)
38 struct utsname utsname;
41 blob_buf_init(&buf, 0);
43 if (uname(&utsname) >= 0)
45 blobmsg_add_string(&buf, "kernel", utsname.release);
46 blobmsg_add_string(&buf, "hostname", utsname.nodename);
49 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
51 while(fgets(line, sizeof(line), f))
53 key = strtok(line, "\t:");
54 val = strtok(NULL, "\t\n");
59 if (!strcasecmp(key, "system type") ||
60 !strcasecmp(key, "processor") ||
61 !strcasecmp(key, "model name"))
63 blobmsg_add_string(&buf, "system", val + 2);
71 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL)
73 if (fgets(line, sizeof(line), f))
75 val = strtok(line, "\t\n");
78 blobmsg_add_string(&buf, "model", val);
83 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
85 while(fgets(line, sizeof(line), f))
87 key = strtok(line, "\t:");
88 val = strtok(NULL, "\t\n");
93 if (!strcasecmp(key, "machine") ||
94 !strcasecmp(key, "hardware"))
96 blobmsg_add_string(&buf, "model", val + 2);
104 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
106 c = blobmsg_open_table(&buf, "release");
108 while (fgets(line, sizeof(line), f))
110 key = strtok(line, "=\"");
111 val = strtok(NULL, "\"\n");
116 if (!strcasecmp(key, "DISTRIB_ID"))
117 blobmsg_add_string(&buf, "distribution", val);
118 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
119 blobmsg_add_string(&buf, "version", val);
120 else if (!strcasecmp(key, "DISTRIB_REVISION"))
121 blobmsg_add_string(&buf, "revision", val);
122 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
123 blobmsg_add_string(&buf, "codename", val);
124 else if (!strcasecmp(key, "DISTRIB_TARGET"))
125 blobmsg_add_string(&buf, "target", val);
126 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
127 blobmsg_add_string(&buf, "description", val);
130 blobmsg_close_array(&buf, c);
135 ubus_send_reply(ctx, req, buf.head);
137 return UBUS_STATUS_OK;
141 rpc_system_info(struct ubus_context *ctx, struct ubus_object *obj,
142 struct ubus_request_data *req, const char *method,
143 struct blob_attr *msg)
152 if (!(tm = localtime(&now)))
153 return UBUS_STATUS_UNKNOWN_ERROR;
156 return UBUS_STATUS_UNKNOWN_ERROR;
158 blob_buf_init(&buf, 0);
160 blobmsg_add_u32(&buf, "uptime", info.uptime);
161 blobmsg_add_u32(&buf, "localtime", mktime(tm));
163 c = blobmsg_open_array(&buf, "load");
164 blobmsg_add_u32(&buf, NULL, info.loads[0]);
165 blobmsg_add_u32(&buf, NULL, info.loads[1]);
166 blobmsg_add_u32(&buf, NULL, info.loads[2]);
167 blobmsg_close_array(&buf, c);
169 c = blobmsg_open_table(&buf, "memory");
170 blobmsg_add_u32(&buf, "total", info.mem_unit * info.totalram);
171 blobmsg_add_u32(&buf, "free", info.mem_unit * info.freeram);
172 blobmsg_add_u32(&buf, "shared", info.mem_unit * info.sharedram);
173 blobmsg_add_u32(&buf, "buffered", info.mem_unit * info.bufferram);
174 blobmsg_close_table(&buf, c);
176 c = blobmsg_open_table(&buf, "swap");
177 blobmsg_add_u32(&buf, "total", info.mem_unit * info.totalswap);
178 blobmsg_add_u32(&buf, "free", info.mem_unit * info.freeswap);
179 blobmsg_close_table(&buf, c);
181 ubus_send_reply(ctx, req, buf.head);
183 return UBUS_STATUS_OK;
186 int rpc_system_api_init(struct ubus_context *ctx)
188 static const struct ubus_method system_methods[] = {
189 UBUS_METHOD_NOARG("board", rpc_system_board),
190 UBUS_METHOD_NOARG("info", rpc_system_info),
193 static struct ubus_object_type system_type =
194 UBUS_OBJECT_TYPE("luci-rpc-system", system_methods);
196 static struct ubus_object obj = {
198 .type = &system_type,
199 .methods = system_methods,
200 .n_methods = ARRAY_SIZE(system_methods),
203 return ubus_add_object(ctx, &obj);