-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmibla.c
More file actions
198 lines (166 loc) · 4.68 KB
/
mibla.c
File metadata and controls
198 lines (166 loc) · 4.68 KB
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
/*
* Copyright (c) 2007-2013 Mikolaj Golub
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id$
*
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "snmp_ucd.h"
/*
* mibla structures and functions.
*/
struct mibla {
int32_t index;
const u_char *name;
u_char load[UCDMAXLEN];
u_char *config;
int32_t loadInt;
int32_t errorFlag;
u_char *errMessage;
};
static struct mibla mibla[3];
static uint64_t last_la_update; /* Ticks of the last la data update. */
static const u_char *la_names[] = {
(const u_char *)"Load-1",
(const u_char *)"Load-5",
(const u_char *)"Load-15"
};
void
mibla_init(void)
{
double sys_la[3];
int i;
if (getloadavg(sys_la, 3) != 3)
syslog(LOG_ERR, "getloadavg failed: %s: %m", __func__);
for (i=0; i < 3; i++) {
mibla[i].index = i + 1;
mibla[i].name = la_names[i];
snprintf((char *)mibla[i].load, sizeof(mibla[i].load), "%.2f",
sys_la[i]);
mibla[i].config = (u_char *)strdup(LACONFIG);
mibla[i].loadInt = (int)(100 * sys_la[i]);
mibla[i].errorFlag = 0;
mibla[i].errMessage = NULL;
}
last_la_update = get_ticks();
}
static void
update_la_data(void)
{
double sys_la[3];
int i;
/* Update data only once in update_interval. */
if ((get_ticks() - last_la_update) > update_interval) {
if (getloadavg(sys_la, 3) != 3)
syslog(LOG_ERR, "getloadavg failed: %s: %m", __func__);
for (i = 0; i < 3; i++) {
float crit;
snprintf ((char *) mibla[i].load, sizeof(mibla[i].load),
"%.2f", sys_la[i]);
mibla[i].loadInt = (int) (100 * sys_la[i]);
crit = strtof((char *) mibla[i].config, NULL);
mibla[i].errorFlag = (crit > 0 && sys_la[i] >= crit);
}
last_la_update = get_ticks();
}
}
int
op_laTable(struct snmp_context * context __unused, struct snmp_value * value,
u_int sub, u_int iidx __unused, enum snmp_op op)
{
asn_subid_t which;
int ret, i;
which = value->var.subs[sub - 1];
switch (op) {
case SNMP_OP_GETNEXT:
i = value->var.subs[sub]++;
if (i > 2)
return (SNMP_ERR_NOSUCHNAME);
value->var.len = sub + 1;
break;
case SNMP_OP_GET:
if (value->var.len - sub != 1)
return (SNMP_ERR_NOSUCHNAME);
if ((i = value->var.subs[sub] - 1) > 2)
return (SNMP_ERR_NOSUCHNAME);
break;
case SNMP_OP_SET:
if (value->var.len - sub != 1)
return (SNMP_ERR_NOSUCHNAME);
if ((i = value->var.subs[sub] - 1) > 2)
return (SNMP_ERR_NOSUCHNAME);
switch(which) {
case LEAF_laConfig:
ret = string_save(value, context, -1,
&mibla[i].config);
return (ret);
case LEAF_laErrMessage:
ret = string_save(value, context, -1,
&mibla[i].errMessage);
return (ret);
default:
break;
}
return (SNMP_ERR_NOT_WRITEABLE);
case SNMP_OP_ROLLBACK:
case SNMP_OP_COMMIT:
return (SNMP_ERR_NOERROR);
default:
return (SNMP_ERR_RES_UNAVAIL);
}
update_la_data();
ret = SNMP_ERR_NOERROR;
switch (which) {
case LEAF_laIndex:
value->v.integer = mibla[i].index;
break;
case LEAF_laNames:
ret = string_get(value, mibla[i].name, -1);
break;
case LEAF_laLoad:
ret = string_get(value, mibla[i].load, -1);
break;
case LEAF_laConfig:
ret = string_get(value, mibla[i].config, -1);
break;
case LEAF_laLoadInt:
value->v.integer = mibla[i].loadInt;
break;
case LEAF_laErrorFlag:
value->v.integer = mibla[i].errorFlag;
break;
case LEAF_laErrMessage:
ret = string_get(value, mibla[i].errMessage, -1);
break;
default:
ret = SNMP_ERR_RES_UNAVAIL;
break;
}
return (ret);
};