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