and added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / mibgroup / ucd-snmp / file.c
1 #include <net-snmp/net-snmp-config.h>
2
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #if TIME_WITH_SYS_TIME
6 # ifdef WIN32
7 #  include <sys/timeb.h>
8 # else
9 #  include <sys/time.h>
10 # endif
11 # include <time.h>
12 #else
13 # if HAVE_SYS_TIME_H
14 #  include <sys/time.h>
15 # else
16 #  include <time.h>
17 # endif
18 #endif
19 #if HAVE_WINSOCK_H
20 #include <winsock.h>
21 #endif
22 #if HAVE_STRING_H
23 #include <string.h>
24 #endif
25
26 #if HAVE_STRING_H
27 #include <string.h>
28 #endif
29
30 #include <net-snmp/net-snmp-includes.h>
31 #include <net-snmp/agent/net-snmp-agent-includes.h>
32
33 #include "struct.h"
34 #include "file.h"
35 #include "util_funcs.h"
36
37 #if HAVE_DMALLOC_H
38 #include <dmalloc.h>
39 #endif
40
41 #define MAXFILE   20
42
43 struct filestat fileTable[MAXFILE];
44 int             fileCount;
45
46 void
47 init_file(void)
48 {
49     struct variable2 file_table[] = {
50         {FILE_INDEX, ASN_INTEGER, RONLY, var_file_table, 1, {1}},
51         {FILE_NAME, ASN_OCTET_STR, RONLY, var_file_table, 1, {2}},
52         {FILE_SIZE, ASN_INTEGER, RONLY, var_file_table, 1, {3}},
53         {FILE_MAX, ASN_INTEGER, RONLY, var_file_table, 1, {4}},
54         {FILE_ERROR, ASN_INTEGER, RONLY, var_file_table, 1, {100}},
55         {FILE_MSG, ASN_OCTET_STR, RONLY, var_file_table, 1, {101}}
56     };
57
58     /*
59      * Define the OID pointer to the top of the mib tree that we're
60      * registering underneath 
61      */
62     oid             file_variables_oid[] = { UCDAVIS_MIB, 15, 1 };
63
64     /*
65      * register ourselves with the agent to handle our mib tree 
66      */
67     REGISTER_MIB("ucd-snmp/file", file_table, variable2,
68                  file_variables_oid);
69
70     snmpd_register_config_handler("file", file_parse_config,
71                                   file_free_config, "file [maxsize]");
72
73 }
74
75 void
76 file_free_config(void)
77 {
78     fileCount = 0;
79 }
80
81 void
82 file_parse_config(const char *token, char *cptr)
83 {
84     if (fileCount < MAXFILE) {
85         fileTable[fileCount].max = -1;
86
87         sscanf(cptr, "%s %d",
88                fileTable[fileCount].name, &fileTable[fileCount].max);
89
90         fileCount++;
91     }
92 }
93
94 void
95 updateFile(int iindex)
96 {
97     struct stat     sb;
98
99     if (stat(fileTable[iindex].name, &sb) == 0)
100         fileTable[iindex].size = sb.st_size >> 10;
101 }
102
103 /*
104  * OID functions 
105  */
106
107 u_char         *
108 var_file_table(struct variable *vp,
109                oid * name,
110                size_t * length,
111                int exact, size_t * var_len, WriteMethod ** write_method)
112 {
113     static long     long_ret;
114     static char     error[256];
115     int             iindex;
116     struct filestat *file;
117
118     if (header_simple_table
119         (vp, name, length, exact, var_len, write_method, fileCount))
120         return (NULL);
121
122     iindex = name[*length - 1] - 1;
123
124     updateFile(iindex);
125
126     file = &fileTable[iindex];
127
128     switch (vp->magic) {
129     case FILE_INDEX:
130         long_ret = iindex + 1;
131         return (u_char *) & long_ret;
132
133     case FILE_NAME:
134         *var_len = strlen(file->name);
135         return (u_char *) file->name;
136
137     case FILE_SIZE:
138         long_ret = file->size;
139         return (u_char *) & long_ret;
140
141     case FILE_MAX:
142         long_ret = file->max;
143         return (u_char *) & long_ret;
144
145     case FILE_ERROR:
146         if (file->max >= 0 && file->size > file->max)
147             long_ret = 1;
148         else
149             long_ret = 0;
150
151         return (u_char *) & long_ret;
152
153     case FILE_MSG:
154         if (file->max >= 0 && file->size > file->max)
155             sprintf(error, FILE_ERROR_MSG, file->name, file->max,
156                     file->size);
157         else
158             strcpy(error, "");
159
160         *var_len = strlen(error);
161         return (u_char *) error;
162
163     default:
164         DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_file_table\n",
165                     vp->magic));
166     }
167
168     return NULL;
169 }