]>
git.proxmox.com Git - mirror_frr.git/blob - lib/agentx.c
2 * Copyright (C) 2012 Vincent Bernat <bernat@luffy.cx>
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 #include <net-snmp/net-snmp-config.h>
26 #include <net-snmp/net-snmp-includes.h>
27 #include <net-snmp/agent/net-snmp-agent-includes.h>
28 #include <net-snmp/agent/snmp_vars.h>
36 static int agentx_enabled
= 0;
38 static struct thread_master
*agentx_tm
;
39 static struct thread
*timeout_thr
= NULL
;
40 static struct list
*events
= NULL
;
42 static void agentx_events_update(void);
45 agentx_timeout(struct thread
*t
)
51 netsnmp_check_outstanding_agent_requests ();
52 agentx_events_update ();
57 agentx_read(struct thread
*t
)
60 struct listnode
*ln
= THREAD_ARG (t
);
61 list_delete_node (events
, ln
);
64 FD_SET (THREAD_FD (t
), &fds
);
67 netsnmp_check_outstanding_agent_requests ();
68 agentx_events_update ();
73 agentx_events_update(void)
77 struct timeval timeout
= { .tv_sec
= 0, .tv_usec
= 0 };
83 THREAD_OFF (timeout_thr
);
86 snmp_select_info (&maxfd
, &fds
, &timeout
, &block
);
89 timeout_thr
= thread_add_timer_tv (agentx_tm
, agentx_timeout
, NULL
, &timeout
);
91 ln
= listhead (events
);
92 thr
= ln
? listgetdata (ln
) : NULL
;
93 thr_fd
= thr
? THREAD_FD (thr
) : -1;
95 /* "two-pointer" / two-list simultaneous iteration
96 * ln/thr/thr_fd point to the next existing event listener to hit while
97 * fd counts to catch up */
98 for (fd
= 0; fd
< maxfd
; fd
++)
103 struct listnode
*nextln
= listnextnode (ln
);
104 if (!FD_ISSET (fd
, &fds
))
107 list_delete_node (events
, ln
);
110 thr
= ln
? listgetdata (ln
) : NULL
;
111 thr_fd
= thr
? THREAD_FD (thr
) : -1;
113 /* need listener, but haven't hit one where it would be */
114 else if (FD_ISSET (fd
, &fds
))
116 struct listnode
*newln
;
117 thr
= thread_add_read (agentx_tm
, agentx_read
, NULL
, fd
);
118 newln
= listnode_add_before (events
, ln
, thr
);
123 /* leftover event listeners at this point have fd > maxfd, delete them */
126 struct listnode
*nextln
= listnextnode (ln
);
127 thread_cancel (listgetdata (ln
));
128 list_delete_node (events
, ln
);
134 static struct cmd_node agentx_node
=
137 "", /* AgentX has no interface. */
141 /* Logging NetSNMP messages */
143 agentx_log_callback(int major
, int minor
,
144 void *serverarg
, void *clientarg
)
146 struct snmp_log_message
*slm
= (struct snmp_log_message
*)serverarg
;
147 char *msg
= XSTRDUP(MTYPE_TMP
, slm
->msg
);
148 if (msg
) msg
[strlen(msg
)-1] = '\0';
149 switch (slm
->priority
)
151 case LOG_EMERG
: zlog_err ("snmp[emerg]: %s", msg
?msg
:slm
->msg
); break;
152 case LOG_ALERT
: zlog_err ("snmp[alert]: %s", msg
?msg
:slm
->msg
); break;
153 case LOG_CRIT
: zlog_err ("snmp[crit]: %s", msg
?msg
:slm
->msg
); break;
154 case LOG_ERR
: zlog_err ("snmp[err]: %s", msg
?msg
:slm
->msg
); break;
155 case LOG_WARNING
: zlog_warn ("snmp[warning]: %s", msg
?msg
:slm
->msg
); break;
156 case LOG_NOTICE
: zlog_notice("snmp[notice]: %s", msg
?msg
:slm
->msg
); break;
157 case LOG_INFO
: zlog_info ("snmp[info]: %s", msg
?msg
:slm
->msg
); break;
158 case LOG_DEBUG
: zlog_debug ("snmp[debug]: %s", msg
?msg
:slm
->msg
); break;
160 XFREE(MTYPE_TMP
, msg
);
161 return SNMP_ERR_NOERROR
;
165 config_write_agentx (struct vty
*vty
)
168 vty_out (vty
, "agentx%s", VTY_NEWLINE
);
172 DEFUN (agentx_enable
,
175 "SNMP AgentX protocol settings\n"
176 "SNMP AgentX settings\n")
180 init_snmp(FRR_SMUX_NAME
);
182 agentx_events_update ();
186 vty_out (vty
, "SNMP AgentX already enabled%s", VTY_NEWLINE
);
194 "SNMP AgentX protocol settings\n"
195 "SNMP AgentX settings\n")
197 if (!agentx_enabled
) return CMD_SUCCESS
;
198 vty_out (vty
, "SNMP AgentX support cannot be disabled once enabled%s", VTY_NEWLINE
);
203 smux_init (struct thread_master
*tm
)
207 netsnmp_enable_subagent ();
209 snmp_enable_calllog ();
210 snmp_register_callback (SNMP_CALLBACK_LIBRARY
,
211 SNMP_CALLBACK_LOGGING
,
214 init_agent (FRR_SMUX_NAME
);
216 install_node (&agentx_node
, config_write_agentx
);
217 install_element (CONFIG_NODE
, &agentx_enable_cmd
);
218 install_element (CONFIG_NODE
, &no_agentx_cmd
);
222 smux_register_mib (const char *descr
, struct variable
*var
,
223 size_t width
, int num
,
224 oid name
[], size_t namelen
)
226 register_mib (descr
, var
, width
, num
, name
, namelen
);
230 smux_trap (struct variable
*vp
, size_t vp_len
,
231 const oid
*ename
, size_t enamelen
,
232 const oid
*name
, size_t namelen
,
233 const oid
*iname
, size_t inamelen
,
234 const struct trap_object
*trapobj
, size_t trapobjlen
,
237 oid objid_snmptrap
[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
238 size_t objid_snmptrap_len
= sizeof objid_snmptrap
/ sizeof (oid
);
239 oid notification_oid
[MAX_OID_LEN
];
240 size_t notification_oid_len
;
243 netsnmp_variable_list
*notification_vars
= NULL
;
244 if (!agentx_enabled
) return 0;
247 oid_copy (notification_oid
, ename
, enamelen
);
248 notification_oid
[enamelen
] = sptrap
;
249 notification_oid_len
= enamelen
+ 1;
250 snmp_varlist_add_variable (¬ification_vars
,
251 objid_snmptrap
, objid_snmptrap_len
,
253 (u_char
*) notification_oid
,
254 notification_oid_len
* sizeof(oid
));
256 /* Provided bindings */
257 for (i
= 0; i
< trapobjlen
; i
++)
260 oid oid
[MAX_OID_LEN
];
261 size_t oid_len
, onamelen
;
264 WriteMethod
*wm
= NULL
;
268 if (trapobj
[i
].namelen
> 0)
270 /* Columnar object */
271 onamelen
= trapobj
[i
].namelen
;
272 oid_copy (oid
, name
, namelen
);
273 oid_copy (oid
+ namelen
, trapobj
[i
].name
, onamelen
);
274 oid_copy (oid
+ namelen
+ onamelen
, iname
, inamelen
);
275 oid_len
= namelen
+ onamelen
+ inamelen
;
280 onamelen
= trapobj
[i
].namelen
* (-1);
281 oid_copy (oid
, name
, namelen
);
282 oid_copy (oid
+ namelen
, trapobj
[i
].name
, onamelen
);
283 oid
[onamelen
+ namelen
] = 0;
284 oid_len
= namelen
+ onamelen
+ 1;
287 /* Locate the appropriate function and type in the MIB registry. */
288 for (j
= 0; j
< vp_len
; j
++)
290 if (oid_compare (trapobj
[i
].name
, onamelen
, vp
[j
].name
, vp
[j
].namelen
) != 0)
292 /* We found the appropriate variable in the MIB registry. */
293 oid_copy(cvp
.name
, name
, namelen
);
294 oid_copy(cvp
.name
+ namelen
, vp
[j
].name
, vp
[j
].namelen
);
295 cvp
.namelen
= namelen
+ vp
[j
].namelen
;
296 cvp
.type
= vp
[j
].type
;
297 cvp
.magic
= vp
[j
].magic
;
299 cvp
.findVar
= vp
[j
].findVar
;
300 /* Grab the result. */
301 val
= cvp
.findVar (&cvp
, oid
, &oid_len
, 1, &val_len
, &wm
);
303 snmp_varlist_add_variable (¬ification_vars
,
313 send_v2trap (notification_vars
);
314 snmp_free_varbind (notification_vars
);
315 agentx_events_update ();
319 #endif /* SNMP_AGENTX */