| 1 |
/* |
| 2 |
* Host Resources MIB - proc processor group implementation - hr_proc.c |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
#include <net-snmp/net-snmp-includes.h> |
| 8 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 9 |
#ifdef HAVE_STDLIB_H |
| 10 |
#include <stdlib.h> |
| 11 |
#endif |
| 12 |
#ifdef HAVE_STRING_H |
| 13 |
#include <string.h> |
| 14 |
#else |
| 15 |
#include <strings.h> |
| 16 |
#endif |
| 17 |
#include <ctype.h> |
| 18 |
|
| 19 |
#include "host_res.h" |
| 20 |
#include "hr_proc.h" |
| 21 |
#include <net-snmp/agent/auto_nlist.h> |
| 22 |
#include <net-snmp/agent/agent_read_config.h> |
| 23 |
#include <net-snmp/agent/hardware/cpu.h> |
| 24 |
#include "ucd-snmp/loadave.h" |
| 25 |
|
| 26 |
#define HRPROC_MONOTONICALLY_INCREASING |
| 27 |
|
| 28 |
/********************* |
| 29 |
* |
| 30 |
* Kernel & interface information, |
| 31 |
* and internal forward declarations |
| 32 |
* |
| 33 |
*********************/ |
| 34 |
|
| 35 |
extern void Init_HR_Proc(void); |
| 36 |
extern int Get_Next_HR_Proc(void); |
| 37 |
const char *describe_proc(int); |
| 38 |
int proc_status(int); |
| 39 |
int header_hrproc(struct variable *, oid *, size_t *, int, |
| 40 |
size_t *, WriteMethod **); |
| 41 |
|
| 42 |
|
| 43 |
/********************* |
| 44 |
* |
| 45 |
* Initialisation & common implementation functions |
| 46 |
* |
| 47 |
*********************/ |
| 48 |
netsnmp_cpu_info *HRP_cpu; |
| 49 |
|
| 50 |
#define HRPROC_ID 1 |
| 51 |
#define HRPROC_LOAD 2 |
| 52 |
|
| 53 |
struct variable4 hrproc_variables[] = { |
| 54 |
{HRPROC_ID, ASN_OBJECT_ID, NETSNMP_OLDAPI_RONLY, |
| 55 |
var_hrproc, 2, {1, 1}}, |
| 56 |
{HRPROC_LOAD, ASN_INTEGER, NETSNMP_OLDAPI_RONLY, |
| 57 |
var_hrproc, 2, {1, 2}} |
| 58 |
}; |
| 59 |
oid hrproc_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 3, 3 }; |
| 60 |
|
| 61 |
|
| 62 |
void |
| 63 |
init_hr_proc(void) |
| 64 |
{ |
| 65 |
init_device[HRDEV_PROC] = Init_HR_Proc; |
| 66 |
next_device[HRDEV_PROC] = Get_Next_HR_Proc; |
| 67 |
device_descr[HRDEV_PROC] = describe_proc; |
| 68 |
device_status[HRDEV_PROC] = proc_status; |
| 69 |
#ifdef HRPROC_MONOTONICALLY_INCREASING |
| 70 |
dev_idx_inc[HRDEV_PROC] = 1; |
| 71 |
#endif |
| 72 |
|
| 73 |
REGISTER_MIB("host/hr_proc", hrproc_variables, variable4, |
| 74 |
hrproc_variables_oid); |
| 75 |
} |
| 76 |
|
| 77 |
/* |
| 78 |
* header_hrproc(... |
| 79 |
* Arguments: |
| 80 |
* vp IN - pointer to variable entry that points here |
| 81 |
* name IN/OUT - IN/name requested, OUT/name found |
| 82 |
* length IN/OUT - length of IN/OUT oid's |
| 83 |
* exact IN - TRUE if an exact match was requested |
| 84 |
* var_len OUT - length of variable or 0 if function returned |
| 85 |
* write_method |
| 86 |
* |
| 87 |
*/ |
| 88 |
|
| 89 |
int |
| 90 |
header_hrproc(struct variable *vp, |
| 91 |
oid * name, |
| 92 |
size_t * length, |
| 93 |
int exact, size_t * var_len, WriteMethod ** write_method) |
| 94 |
{ |
| 95 |
#define HRPROC_ENTRY_NAME_LENGTH 11 |
| 96 |
oid newname[MAX_OID_LEN]; |
| 97 |
int proc_idx, LowIndex = -1; |
| 98 |
int result; |
| 99 |
|
| 100 |
DEBUGMSGTL(("host/hr_proc", "var_hrproc: ")); |
| 101 |
DEBUGMSGOID(("host/hr_proc", name, *length)); |
| 102 |
DEBUGMSG(("host/hr_proc", " %d\n", exact)); |
| 103 |
|
| 104 |
memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid)); |
| 105 |
/* |
| 106 |
* Find "next" proc entry |
| 107 |
*/ |
| 108 |
|
| 109 |
Init_HR_Proc(); |
| 110 |
for (;;) { |
| 111 |
proc_idx = Get_Next_HR_Proc(); |
| 112 |
if (proc_idx == -1) |
| 113 |
break; |
| 114 |
newname[HRPROC_ENTRY_NAME_LENGTH] = proc_idx; |
| 115 |
result = snmp_oid_compare(name, *length, newname, vp->namelen + 1); |
| 116 |
if (exact && (result == 0)) { |
| 117 |
LowIndex = proc_idx; |
| 118 |
/* |
| 119 |
* Save processor status information |
| 120 |
*/ |
| 121 |
break; |
| 122 |
} |
| 123 |
if ((!exact && (result < 0)) && |
| 124 |
(LowIndex == -1 || proc_idx < LowIndex)) { |
| 125 |
LowIndex = proc_idx; |
| 126 |
/* |
| 127 |
* Save processor status information |
| 128 |
*/ |
| 129 |
#ifdef HRPROC_MONOTONICALLY_INCREASING |
| 130 |
break; |
| 131 |
#endif |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if (LowIndex == -1) { |
| 136 |
DEBUGMSGTL(("host/hr_proc", "... index out of range\n")); |
| 137 |
return (MATCH_FAILED); |
| 138 |
} |
| 139 |
|
| 140 |
memcpy((char *) name, (char *) newname, |
| 141 |
(vp->namelen + 1) * sizeof(oid)); |
| 142 |
*length = vp->namelen + 1; |
| 143 |
*write_method = (WriteMethod*)0; |
| 144 |
*var_len = sizeof(long); /* default to 'long' results */ |
| 145 |
|
| 146 |
DEBUGMSGTL(("host/hr_proc", "... get proc stats ")); |
| 147 |
DEBUGMSGOID(("host/hr_proc", name, *length)); |
| 148 |
DEBUGMSG(("host/hr_proc", "\n")); |
| 149 |
return LowIndex; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/********************* |
| 154 |
* |
| 155 |
* System specific implementation functions |
| 156 |
* |
| 157 |
*********************/ |
| 158 |
|
| 159 |
|
| 160 |
u_char * |
| 161 |
var_hrproc(struct variable * vp, |
| 162 |
oid * name, |
| 163 |
size_t * length, |
| 164 |
int exact, size_t * var_len, WriteMethod ** write_method) |
| 165 |
{ |
| 166 |
int proc_idx; |
| 167 |
unsigned long long value; |
| 168 |
netsnmp_cpu_info *cpu; |
| 169 |
|
| 170 |
proc_idx = |
| 171 |
header_hrproc(vp, name, length, exact, var_len, write_method); |
| 172 |
if (proc_idx == MATCH_FAILED) |
| 173 |
return NULL; |
| 174 |
|
| 175 |
switch (vp->magic) { |
| 176 |
case HRPROC_ID: |
| 177 |
*var_len = nullOidLen; |
| 178 |
return (u_char *) nullOid; |
| 179 |
case HRPROC_LOAD: |
| 180 |
cpu = netsnmp_cpu_get_byIdx( proc_idx & HRDEV_TYPE_MASK, 0 ); |
| 181 |
if ( !cpu || !cpu->history || !cpu->history[0].total_hist || |
| 182 |
( cpu->history[0].total_hist == cpu->total_ticks )) |
| 183 |
return NULL; |
| 184 |
|
| 185 |
value = (cpu->idle_ticks - cpu->history[0].idle_hist)*100; |
| 186 |
value /= (cpu->total_ticks - cpu->history[0].total_hist); |
| 187 |
long_return = 100 - value; |
| 188 |
if (long_return < 0) |
| 189 |
long_return = 0; |
| 190 |
return (u_char *) & long_return; |
| 191 |
default: |
| 192 |
DEBUGMSGTL(("host/hr_proc", "unknown sub-id %d in var_hrproc\n", |
| 193 |
vp->magic)); |
| 194 |
} |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
/********************* |
| 200 |
* |
| 201 |
* Internal implementation functions |
| 202 |
* |
| 203 |
*********************/ |
| 204 |
|
| 205 |
void |
| 206 |
Init_HR_Proc(void) |
| 207 |
{ |
| 208 |
HRP_cpu = netsnmp_cpu_get_first(); /* 'Overall' entry */ |
| 209 |
} |
| 210 |
|
| 211 |
int |
| 212 |
Get_Next_HR_Proc(void) |
| 213 |
{ |
| 214 |
HRP_cpu = netsnmp_cpu_get_next( HRP_cpu ); |
| 215 |
if ( HRP_cpu ) |
| 216 |
return (HRDEV_PROC << HRDEV_TYPE_SHIFT) + HRP_cpu->idx; |
| 217 |
else |
| 218 |
return -1; |
| 219 |
} |
| 220 |
|
| 221 |
const char * |
| 222 |
describe_proc(int idx) |
| 223 |
{ |
| 224 |
netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( idx & HRDEV_TYPE_MASK, 0 ); |
| 225 |
return (cpu ? cpu->descr : NULL ); |
| 226 |
} |
| 227 |
|
| 228 |
int |
| 229 |
proc_status(int idx) |
| 230 |
{ |
| 231 |
netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( idx & HRDEV_TYPE_MASK, 0 ); |
| 232 |
return (cpu ? cpu->status : 0 ); |
| 233 |
} |