-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtkaclMIB.c
227 lines (181 loc) · 7.93 KB
/
xtkaclMIB.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Note: this file originally auto-generated by mib2c using
* $
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "xtkaclMIB.h"
#include <time.h> // current time
#include <sys/sysinfo.h> // get system information(int sysinfo(struct sysinfo *info))
#include <stdint.h> // uint64_t
#define SIZE_IN_MB 1048576 // The number used for conversion Byte number to higher unit MegaBytes (used type of conversion 1kB == 1024B not 1kB == 1000B )
struct Number {
int32_t val;
};
struct Number number = { .val = 0 };
/** Initializes the xtkaclMIB module */
void
init_xtkaclMIB(void)
{
const oid myLogin_oid[] = { 1,3,6,1,3,22,1 };
const oid myCurrentTime_oid[] = { 1,3,6,1,3,22,2 };
const oid myNumGetSet_oid[] = { 1,3,6,1,3,22,3 };
const oid myRAM_oid[] = { 1,3,6,1,3,22,4 };
DEBUGMSGTL(("xtkaclMIB", "Initializing\n"));
netsnmp_register_scalar(
netsnmp_create_handler_registration("myLogin", handle_myLogin,
myLogin_oid, OID_LENGTH(myLogin_oid),
HANDLER_CAN_RONLY
));
netsnmp_register_scalar(
netsnmp_create_handler_registration("myCurrentTime", handle_myCurrentTime,
myCurrentTime_oid, OID_LENGTH(myCurrentTime_oid),
HANDLER_CAN_RONLY
));
netsnmp_register_scalar(
netsnmp_create_handler_registration("myNumGetSet", handle_myNumGetSet,
myNumGetSet_oid, OID_LENGTH(myNumGetSet_oid),
HANDLER_CAN_RWRITE
));
netsnmp_register_scalar(
netsnmp_create_handler_registration("myRAM", handle_myRAM,
myRAM_oid, OID_LENGTH(myRAM_oid),
HANDLER_CAN_RONLY
));
}
int
handle_myLogin(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
/* We are never called for a GETNEXT if it's registered as a
"instance", as it's "magically" handled for us. */
/* a instance handler also only hands us one request at a time, so
we don't need to loop over a list of requests; we'll only get one. */
// Static used beacause this object is only read-only.
static char login[] = "xtkacl00";
static int login_size = sizeof(login)-1;
switch(reqinfo->mode) {
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR, &login, login_size);
break;
default:
/* we should never get here, so this is a really bad error */
snmp_log(LOG_ERR, "unknown mode (%d) in handle_myLogin\n", reqinfo->mode );
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}
int
handle_myCurrentTime(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
/* We are never called for a GETNEXT if it's registered as a
"instance", as it's "magically" handled for us. */
/* a instance handler also only hands us one request at a time, so
we don't need to loop over a list of requests; we'll only get one. */
time_t current_time;
char time_buff[25];
static int time_format_size = 20; // fixed size of output time format
time(¤t_time); // get current time calendar time as a object of data type type_t
// Get string representation of time in GMT time zone +00:00 with RFC-3339 formatting.
strftime(time_buff,time_format_size+1,"%Y-%m-%dT%H:%M:%SZ", gmtime(¤t_time)); // time_format_size + 1 beacuse '\0'
switch(reqinfo->mode) {
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR, time_buff, time_format_size);
break;
default:
/* we should never get here, so this is a really bad error */
snmp_log(LOG_ERR, "unknown mode (%d) in handle_myCurrentTime\n", reqinfo->mode );
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}
int
handle_myNumGetSet(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
;
/* We are never called for a GETNEXT if it's registered as a
"instance", as it's "magically" handled for us. */
/* a instance handler also only hands us one request at a time, so
we don't need to loop over a list of requests; we'll only get one. */
switch(reqinfo->mode) {
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER, &number.val, sizeof(number.val));
break;
/*
* SET REQUEST
*
* multiple states in the transaction. See:
* http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg
*/
case MODE_SET_RESERVE1:
/* or you could use netsnmp_check_vb_type_and_size instead */
number.val = netsnmp_check_vb_type(requests->requestvb, ASN_INTEGER);
if ( number.val != SNMP_ERR_NOERROR ) {
netsnmp_set_request_error(reqinfo, requests, number.val );
}
break;
case MODE_SET_RESERVE2:
break;
case MODE_SET_FREE:
break;
case MODE_SET_ACTION:
number.val = *(requests->requestvb->val.integer); //store new value which was accepted by snmpset for agent module
break;
case MODE_SET_COMMIT:
break;
case MODE_SET_UNDO:
break;
default:
/* we should never get here, so this is a really bad error */
snmp_log(LOG_ERR, "unknown mode (%d) in handle_myNumGetSet\n", reqinfo->mode );
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}
int
handle_myRAM(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
/* We are never called for a GETNEXT if it's registered as a
"instance", as it's "magically" handled for us. */
/* a instance handler also only hands us one request at a time, so
we don't need to loop over a list of requests; we'll only get one. */
struct sysinfo system_info; // declare system info data structure
sysinfo(&system_info); // determine system info into sysinfo data stucture
char sys_buff[30]; // char buffer with fixed size for result value
memset(sys_buff, 0, sizeof(sys_buff)); // clear buffer
uint64_t ram_size_mb = SIZE_IN_MB; // MAGIC number for B->MB conversion
uint64_t ram_in_mb = system_info.totalram / ram_size_mb; // compute total ram size in bytes to size in megabytes
sprintf(sys_buff, "%"PRIu64" MB", ram_in_mb); // push value into buffer with unit name of value
/**
* Via OCTET STRING are printable characters as '/0',
* so i decided that i am going to count text size without them). :)
*/
int i = 0;
int text_size = 0;
while(sys_buff[i] != '\0') {
text_size++;
i++;
}
switch(reqinfo->mode) {
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR, &sys_buff, sizeof(char) * text_size);
break;
default:
/* we should never get here, so this is a really bad error */
snmp_log(LOG_ERR, "unknown mode (%d) in handle_myRAM\n", reqinfo->mode );
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}