da3d102ce858068eafd2d7deaf12448d0121b027
[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"));
75
76         ret->free_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "free-memory"));
77         ret->total_memory = sstrtoui64 (ros_reply_param_val_by_key (r, "total-memory"));
78
79         ret->free_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "free-hdd-space"));
80         ret->total_hdd_space = sstrtoui64 (ros_reply_param_val_by_key (r, "total-hdd-space"));
81
82         ret->write_sect_since_reboot = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-since-reboot"));
83         ret->write_sect_total = sstrtoui64 (ros_reply_param_val_by_key (r, "write-sect-total"));
84         ret->bad_blocks = sstrtoui64 (ros_reply_param_val_by_key (r, "bad-blocks"));
85
86         return (0);
87 } /* }}} int rt_reply_to_system_resource */
88
89 static int sr_internal_handler (ros_connection_t *c, /* {{{ */
90                 const ros_reply_t *r, void *user_data)
91 {
92         ros_system_resource_t sys_res;
93         rt_internal_data_t *internal_data;
94         int status;
95
96         memset (&sys_res, 0, sizeof (sys_res));
97         status = rt_reply_to_system_resource (r, &sys_res);
98         if (status != 0)
99                 return (status);
100
101         internal_data = user_data;
102
103         status = internal_data->handler (c, &sys_res, internal_data->user_data);
104
105         return (status);
106 } /* }}} int sr_internal_handler */
107
108 /*
109  * Public functions
110  */
111 int ros_system_resource (ros_connection_t *c, /* {{{ */
112                 ros_system_resource_handler_t handler, void *user_data)
113 {
114         rt_internal_data_t data;
115
116         if ((c == NULL) || (handler == NULL))
117                 return (EINVAL);
118
119         data.handler = handler;
120         data.user_data = user_data;
121
122         return (ros_query (c, "/system/resource/print",
123                                 /* args_num = */ 0, /* args = */ NULL,
124                                 sr_internal_handler, &data));
125 } /* }}} int ros_system_resource */
126
127 /* vim: set ts=2 sw=2 noet fdm=marker : */