1 #include <net-snmp/net-snmp-config.h>
2 
3 #if HAVE_STDLIB_H
4 #include <stdlib.h>
5 #endif
6 #if HAVE_UNISTD_H
7 #include <unistd.h>
8 #endif
9 #if HAVE_FCNTL_H
10 #include <fcntl.h>
11 #endif
12 #include <signal.h>
13 #if TIME_WITH_SYS_TIME
14 # include <sys/time.h>
15 # include <time.h>
16 #else
17 # if HAVE_SYS_TIME_H
18 #  include <sys/time.h>
19 # else
20 #  include <time.h>
21 # endif
22 #endif
23 #if HAVE_MACHINE_PARAM_H
24 #include <machine/param.h>
25 #endif
26 #if HAVE_SYS_PARAM_H
27 #include <sys/param.h>
28 #endif
29 #if HAVE_SYS_VMMETER_H
30 #if !(defined(bsdi2) || defined(netbsd1))
31 #include <sys/vmmeter.h>
32 #endif
33 #endif
34 #if HAVE_SYS_CONF_H
35 #include <sys/conf.h>
36 #endif
37 #if HAVE_ASM_PAGE_H
38 #include <asm/page.h>
39 #endif
40 #if HAVE_SYS_SWAP_H
41 #include <sys/swap.h>
42 #endif
43 #if HAVE_SYS_FS_H
44 #include <sys/fs.h>
45 #else
46 #if HAVE_UFS_FS_H
47 #include <ufs/fs.h>
48 #else
49 #ifdef HAVE_SYS_STAT_H
50 #include <sys/stat.h>
51 #endif
52 #if !defined(dragonfly)
53 #ifdef HAVE_SYS_VNODE_H
54 #include <sys/vnode.h>
55 #endif
56 #endif
57 #ifdef HAVE_UFS_UFS_QUOTA_H
58 #include <ufs/ufs/quota.h>
59 #endif
60 #ifdef HAVE_UFS_UFS_INODE_H
61 #include <ufs/ufs/inode.h>
62 #endif
63 #if HAVE_UFS_FFS_FS_H
64 #include <ufs/ffs/fs.h>
65 #endif
66 #endif
67 #endif
68 #if HAVE_MTAB_H
69 #include <mtab.h>
70 #endif
71 #include <errno.h>
72 #if HAVE_FSTAB_H
73 #include <fstab.h>
74 #endif
75 #if HAVE_SYS_STATFS_H
76 #include <sys/statfs.h>
77 #endif
78 #if HAVE_SYS_STATVFS_H
79 #include <sys/statvfs.h>
80 #endif
81 #if HAVE_SYS_VFS_H
82 #include <sys/vfs.h>
83 #endif
84 #if (!defined(HAVE_STATVFS)) && defined(HAVE_STATFS)
85 #if HAVE_SYS_PARAM_H
86 #include <sys/param.h>
87 #endif
88 #if HAVE_SYS_MOUNT_H
89 #include <sys/mount.h>
90 #endif
91 #if HAVE_SYS_SYSCTL_H
92 #include <sys/sysctl.h>
93 #endif
94 #define statvfs statfs
95 #endif
96 #if HAVE_VM_VM_H
97 #include <vm/vm.h>
98 #endif
99 #if HAVE_VM_SWAP_PAGER_H
100 #include <vm/swap_pager.h>
101 #endif
102 #if HAVE_SYS_FIXPOINT_H
103 #include <sys/fixpoint.h>
104 #endif
105 #if HAVE_MALLOC_H
106 #include <malloc.h>
107 #endif
108 #if HAVE_STRING_H
109 #include <string.h>
110 #endif
111 
112 #include <net-snmp/net-snmp-includes.h>
113 #include <net-snmp/agent/net-snmp-agent-includes.h>
114 #include <net-snmp/agent/auto_nlist.h>
115 
116 #include "struct.h"
117 #include "errormib.h"
118 #include "util_funcs/header_generic.h"
119 
120 static time_t   errorstatustime = 0;
121 static int      errorstatusprior = 0;
122 static char     errorstring[STRMAX];
123 
124 void
setPerrorstatus(const char * to)125 setPerrorstatus(const char *to)
126 {
127     char            buf[STRMAX];
128 
129     snprintf(buf, sizeof(buf), "%s:  %s", to, strerror(errno));
130     buf[ sizeof(buf)-1 ] = 0;
131     snmp_log_perror(to);
132     seterrorstatus(buf, 5);
133 }
134 
135 void
seterrorstatus(const char * to,int prior)136 seterrorstatus(const char *to, int prior)
137 {
138     if (errorstatusprior <= prior ||
139         (NETSNMP_ERRORTIMELENGTH < (time(NULL) - errorstatustime))) {
140         strlcpy(errorstring, to, sizeof(errorstring));
141         errorstatusprior = prior;
142         errorstatustime = time(NULL);
143     }
144 }
145 
146 void
init_errormib(void)147 init_errormib(void)
148 {
149 
150     /*
151      * define the structure we're going to ask the agent to register our
152      * information at
153      */
154     struct variable2 extensible_error_variables[] = {
155         {MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
156          var_extensible_errors, 1, {MIBINDEX}},
157         {ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
158          var_extensible_errors, 1, {ERRORNAME}},
159         {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
160          var_extensible_errors, 1, {ERRORFLAG}},
161         {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
162          var_extensible_errors, 1, {ERRORMSG}}
163     };
164 
165     /*
166      * Define the OID pointer to the top of the mib tree that we're
167      * registering underneath
168      */
169     oid             extensible_error_variables_oid[] =
170         { NETSNMP_UCDAVIS_MIB, NETSNMP_ERRORMIBNUM };
171 
172     /*
173      * register ourselves with the agent to handle our mib tree
174      */
175     REGISTER_MIB("ucd-snmp/errormib", extensible_error_variables,
176                  variable2, extensible_error_variables_oid);
177 }
178 
179 /*
180  * var_extensible_errors(...
181  * Arguments:
182  * vp     IN      - pointer to variable entry that points here
183  * name    IN/OUT  - IN/name requested, OUT/name found
184  * length  IN/OUT  - length of IN/OUT oid's
185  * exact   IN      - TRUE if an exact match was requested
186  * var_len OUT     - length of variable or 0 if function returned
187  * write_method
188  *
189  */
190 u_char         *
var_extensible_errors(struct variable * vp,oid * name,size_t * length,int exact,size_t * var_len,WriteMethod ** write_method)191 var_extensible_errors(struct variable *vp,
192                       oid * name,
193                       size_t * length,
194                       int exact,
195                       size_t * var_len, WriteMethod ** write_method)
196 {
197 
198     static long     long_ret;
199     static char     errmsg[300];
200 
201 
202     if (header_generic(vp, name, length, exact, var_len, write_method))
203         return (NULL);
204 
205     errmsg[0] = 0;
206 
207     switch (vp->magic) {
208     case MIBINDEX:
209         long_ret = name[*length - 1];
210         return ((u_char *) (&long_ret));
211     case ERRORNAME:
212         strcpy(errmsg, "snmp");
213         *var_len = strlen(errmsg);
214         return ((u_char *) errmsg);
215     case ERRORFLAG:
216         long_ret =
217             (NETSNMP_ERRORTIMELENGTH >= time(NULL) - errorstatustime) ? 1 : 0;
218         return ((u_char *) (&long_ret));
219     case ERRORMSG:
220         if ((NETSNMP_ERRORTIMELENGTH >= time(NULL) - errorstatustime) ? 1 : 0) {
221             strlcpy(errmsg, errorstring, sizeof(errmsg));
222         } else
223             errmsg[0] = 0;
224         *var_len = strlen(errmsg);
225         return ((u_char *) errmsg);
226     }
227     return NULL;
228 }
229