2 * librouteros - src/system_resource.c
3 * Copyright (C) 2009 Florian octo Forster
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 * Florian octo Forster <octo at verplant.org>
21 #ifndef _ISOC99_SOURCE
22 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
39 #include "routeros_api.h"
40 #include "ros_parse.h"
45 struct rt_internal_data_s
47 ros_system_resource_handler_t handler;
50 typedef struct rt_internal_data_s rt_internal_data_t;
55 static int rt_reply_to_system_resource (const ros_reply_t *r, /* {{{ */
56 ros_system_resource_t *ret)
61 if (strcmp ("re", ros_reply_status (r)) != 0)
62 return (rt_reply_to_system_resource (ros_reply_next (r), ret));
64 ret->uptime = sstrtodate (ros_reply_param_val_by_key (r, "uptime"));
66 ret->version = ros_reply_param_val_by_key (r, "version");
67 ret->architecture_name = ros_reply_param_val_by_key (r, "architecture-name");
68 ret->board_name = ros_reply_param_val_by_key (r, "board-name");
70 ret->cpu_model = ros_reply_param_val_by_key (r, "cpu");
71 ret->cpu_count = sstrtoui (ros_reply_param_val_by_key (r, "cpu-count"));
72 ret->cpu_load = sstrtoui (ros_reply_param_val_by_key (r, "cpu-load"));
73 ret->cpu_frequency = sstrtoui64 (ros_reply_param_val_by_key (r, "cpu-frequency")) * 1000000;
75 /* One "kilobyte" is 1024 bytes, according to "janisk", see
76 * <http://forum.mikrotik.com/viewtopic.php?f=2&t=37943> */
77 ret->free_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "free-memory")) * 1024;
78 ret->total_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "total-memory")) * 1024;
80 ret->free_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "free-hdd-space")) * 1024;
81 ret->total_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "total-hdd-space")) * 1024;
83 ret->write_sect_since_reboot = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-since-reboot"));
84 ret->write_sect_total = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-total"));
85 ret->bad_blocks = sstrtoui64 (ros_reply_param_val_by_key (r, "bad-blocks"));
88 } /* }}} int rt_reply_to_system_resource */
90 static int sr_internal_handler (ros_connection_t *c, /* {{{ */
91 const ros_reply_t *r, void *user_data)
93 ros_system_resource_t sys_res;
94 rt_internal_data_t *internal_data;
97 memset (&sys_res, 0, sizeof (sys_res));
98 status = rt_reply_to_system_resource (r, &sys_res);
102 internal_data = user_data;
104 status = internal_data->handler (c, &sys_res, internal_data->user_data);
107 } /* }}} int sr_internal_handler */
112 int ros_system_resource (ros_connection_t *c, /* {{{ */
113 ros_system_resource_handler_t handler, void *user_data)
115 rt_internal_data_t data;
117 if ((c == NULL) || (handler == NULL))
120 data.handler = handler;
121 data.user_data = user_data;
123 return (ros_query (c, "/system/resource/print",
124 /* args_num = */ 0, /* args = */ NULL,
125 sr_internal_handler, &data));
126 } /* }}} int ros_system_resource */
128 /* vim: set ts=2 sw=2 noet fdm=marker : */