f3472b7070677ddb3936b375f7017d6da81caeb9
[routeros-api.git] / src / system_resource.c
1 /**
2  * librouteros - src/interface.c
3  * Copyright (C) 2009  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
24 #endif
25
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
28 #endif
29
30 #include "config.h"
31
32 #include <stdlib.h>
33 #include <math.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <assert.h>
39
40 #include "routeros_api.h"
41 #include "ros_parse.h"
42
43 /*
44  * Private data types
45  */
46 struct rt_internal_data_s
47 {
48         ros_system_resource_handler_t handler;
49         void *user_data;
50 };
51 typedef struct rt_internal_data_s rt_internal_data_t;
52
53 /*
54  * Private functions
55  */
56 static int rt_reply_to_system_resource (const ros_reply_t *r, /* {{{ */
57                 ros_system_resource_t *ret)
58 {
59         if (r == NULL)
60                 return (EINVAL);
61
62         if (strcmp ("re", ros_reply_status (r)) != 0)
63                 return (rt_reply_to_system_resource (ros_reply_next (r), ret));
64
65         ret->uptime = sstrtodate (ros_reply_param_val_by_key (r, "uptime"));
66
67         ret->version = ros_reply_param_val_by_key (r, "version");
68         ret->architecture_name = ros_reply_param_val_by_key (r, "architecture-name");
69         ret->board_name = ros_reply_param_val_by_key (r, "board-name");
70
71         ret->cpu_model = ros_reply_param_val_by_key (r, "cpu");
72         ret->cpu_count = sstrtoui (ros_reply_param_val_by_key (r, "cpu-count"));
73         ret->cpu_load = sstrtoui (ros_reply_param_val_by_key (r, "cpu-load"));
74         ret->cpu_frequency = sstrtoui64 (ros_reply_param_val_by_key (r, "cpu-frequency")) * 1000000;
75
76         /* One "kilobyte" is 1024 bytes, according to "janisk", see
77          * <http://forum.mikrotik.com/viewtopic.php?f=2&t=37943> */
78         ret->free_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "free-memory")) * 1024;
79         ret->total_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "total-memory")) * 1024;
80
81         ret->free_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "free-hdd-space")) * 1024;
82         ret->total_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "total-hdd-space")) * 1024;
83
84         ret->write_sect_since_reboot = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-since-reboot"));
85         ret->write_sect_total = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-total"));
86         ret->bad_blocks = sstrtoui64 (ros_reply_param_val_by_key (r, "bad-blocks"));
87
88         return (0);
89 } /* }}} int rt_reply_to_system_resource */
90
91 static int sr_internal_handler (ros_connection_t *c, /* {{{ */
92                 const ros_reply_t *r, void *user_data)
93 {
94         ros_system_resource_t sys_res;
95         rt_internal_data_t *internal_data;
96         int status;
97
98         memset (&sys_res, 0, sizeof (sys_res));
99         status = rt_reply_to_system_resource (r, &sys_res);
100         if (status != 0)
101                 return (status);
102
103         internal_data = user_data;
104
105         status = internal_data->handler (c, &sys_res, internal_data->user_data);
106
107         return (status);
108 } /* }}} int sr_internal_handler */
109
110 /*
111  * Public functions
112  */
113 int ros_system_resource (ros_connection_t *c, /* {{{ */
114                 ros_system_resource_handler_t handler, void *user_data)
115 {
116         rt_internal_data_t data;
117
118         if ((c == NULL) || (handler == NULL))
119                 return (EINVAL);
120
121         data.handler = handler;
122         data.user_data = user_data;
123
124         return (ros_query (c, "/system/resource/print",
125                                 /* args_num = */ 0, /* args = */ NULL,
126                                 sr_internal_handler, &data));
127 } /* }}} int ros_system_resource */
128
129 /* vim: set ts=2 sw=2 noet fdm=marker : */