操作Solid 数据库代码
solid数据可能听说过的人不多,可是很多unix软件很多都采用此数据库,其儿紧凑,也是不大常见所以也能操持其稳定吧
当初为了操作这个东东也是费了好些时候,主要是要申请到solid的sdk包 libsolidsa.a
有了当然好办了,当然没有的可以联系我
当初为了操作这个东东也是费了好些时候,主要是要申请到solid的sdk包 libsolidsa.a
有了当然好办了,当然没有的可以联系我
1
/*
2 nnm_event_downup detail dump
3 2005.11.07 zhangbin created
4
5 */
6
7 #include < iostream >
8 #include < string >
9 #include < vector >
10 #include < stdio.h >
11 #include < unistd.h >
12
13 #include < c.h >
14 #include < sscapi.h >
15 #include < sa.h >
16
17
18 SaRetT dbrc;
19 SaConnectT * dbscon;
20
21
22 typedef std::string RPSTRING;
23 extern char * optarg;
24 extern int optind, optopt;
25
26
27 struct TableEventDownUp{
28 int TRAPSOURCE;
29 int APPLICATION_ID;
30 int PID;
31 int CATEGORY;
32 int SEVERITY;
33 int EVENT_TIMESTAMP;
34 int NUMBER_VARBINDS;
35 int IFNUMBER;
36 RPSTRING TRAP_NAME;
37 RPSTRING FORWARD_ADDRESS;
38 RPSTRING EVENT_SOURCE;
39 RPSTRING EVENT_TIME;
40 RPSTRING EVENT_UUID;
41 RPSTRING NODENAME;
42 RPSTRING MESSAGE;
43 RPSTRING EVENT_OID;
44 RPSTRING OV_OBJECTID;
45 RPSTRING PROTOCOL;
46 RPSTRING EVENT_TYPE;
47 RPSTRING IP_ADDRESS;
48 // char * fs[18];
49
50 };
51
52 typedef struct _sysconf{
53 RPSTRING solid_host;
54 unsigned short solid_port;
55 RPSTRING solid_user;
56 RPSTRING solid_passwd;
57 RPSTRING dump_file;
58 RPSTRING pattern;
59 FILE * file;
60 std::vector < RPSTRING > fields;
61 _sysconf(){
62 solid_host = " 127.0.0.1 " ;
63 solid_port = 2690 ;
64 solid_user = " ovdb " ;
65 solid_passwd = " ovdb " ;
66 dump_file = " ./event.dump " ;
67 pattern = "" ;
68
69
70 }
71 }SysConf;
72
73 void usage(){
74 char * msg = " usage: "
75 " \n-h solid db host "
76 " \n-p solid db port "
77 " \n-U solid user name "
78 " \n-P solid passwd "
79 " \n-f dump file "
80 " \n-l like pattern "
81 " \n " ;
82 printf( " %s " ,msg);
83 }
84
85
86
87
88 void INT_STRING(SysConf * conf, const char * field,RPSTRING & str, int intv){
89 char buff[ 32 ];
90 if ( std::find(conf -> fields.begin(),conf -> fields.end(),RPSTRING(field) + " \n " ) != conf -> fields.end()){
91 sprintf(buff, " \t%u " ,intv);
92 str += buff;
93 }
94 }
95
96 void STRING_STRING(SysConf * conf, const char * field,RPSTRING & str,RPSTRING str2){
97 if ( std::find(conf -> fields.begin(),conf -> fields.end(),RPSTRING(field) + " \n " ) != conf -> fields.end()){
98 str += " \t " + str2;
99 }
100 }
101
102 void write_file(SysConf * conf,TableEventDownUp row){
103 RPSTRING str = " " ;
104
105 INT_STRING(conf, " TRAPSOURCE " ,str,row.TRAPSOURCE);
106 INT_STRING(conf, " APPLICATION_ID " ,str,row.APPLICATION_ID);
107 INT_STRING(conf, " PID " ,str,row.PID);
108 INT_STRING(conf, " CATEGORY " ,str,row.CATEGORY);
109 INT_STRING(conf, " SEVERITY " ,str,row.SEVERITY);
110 INT_STRING(conf, " EVENT_TIMESTAMP " ,str,row.EVENT_TIMESTAMP);
111 INT_STRING(conf, " NUMBER_VARBINDS " ,str,row.NUMBER_VARBINDS);
112 INT_STRING(conf, " IFNUMBER " ,str,row.IFNUMBER);
113 STRING_STRING(conf, " TRAP_NAME " ,str,row.TRAP_NAME);
114 str += " \t " ;
115 STRING_STRING(conf, " FORWARD_ADDRESS " ,str,row.FORWARD_ADDRESS);
116 STRING_STRING(conf, " EVENT_SOURCE " ,str,row.EVENT_SOURCE);
117 STRING_STRING(conf, " EVENT_TIME " ,str,row.EVENT_TIME);
118 STRING_STRING(conf, " EVENT_UUID " ,str,row.EVENT_UUID);
119 STRING_STRING(conf, " NODENAME " ,str,row.NODENAME);
120 STRING_STRING(conf, " MESSAGE " ,str,row.MESSAGE);
121 STRING_STRING(conf, " EVENT_OID " ,str,row.EVENT_OID);
122 STRING_STRING(conf, " OV_OBJECTID " ,str,row.OV_OBJECTID);
123 STRING_STRING(conf, " PROTOCOL " ,str,row.PROTOCOL);
124 STRING_STRING(conf, " EVENT_TYPE " ,str,row.EVENT_TYPE);
125 STRING_STRING(conf, " IP_ADDRESS " ,str,row.IP_ADDRESS);
126 // -- rid of "LR"
127 std::replace(str.begin(),str.end(), ' \n ' , ' ' );
128 str += " \n " ;
129 // printf("\nrow==>%s",str.c_str());
130 // return;
131 fputs(str.c_str(),conf -> file);
132 }
133
134
135 void dump(SysConf * conf){
136 TableEventDownUp reload;
137 SaRetT rc;
138 SaCursorT * scur;
139 char * EVENT_UUID;
140 int EVENT_TIMESTAMP;
141 int CATEGORY;
142 char * NODENAME;
143 int APPLICATION_ID;
144 char * MESSAGE;
145 int SEVERITY;
146 char * EVENT_OID;
147 char * OV_OBJECTID;
148 char * PROTOCOL;
149 char * EVENT_TYPE;
150 char * IP_ADDRESS;
151 int TRAPSOURCE;
152 char * TRAP_NAME;
153 int PID;
154 char * FORWARD_ADDRESS;
155 char * EVENT_SOURCE;
156 char * EVENT_TIME;
157 int NUMBER_VARBINDS;
158 int IFNUMBER;
159
160 EVENT_TIME = ( char * )conf -> pattern.c_str();
161 scur = SaCursorCreate(dbscon, " NNM_EVENT_DOWNUP " );
162 rc = SaCursorColStr(scur, " EVENT_UUID " , & EVENT_UUID);
163 rc = SaCursorColInt(scur, " EVENT_TIMESTAMP " , & EVENT_TIMESTAMP);
164 rc = SaCursorColInt(scur, " CATEGORY " , & CATEGORY);
165 rc = SaCursorColStr(scur, " NODENAME " , & NODENAME);
166 rc = SaCursorColInt(scur, " APPLICATION_ID " , & APPLICATION_ID);
167 rc = SaCursorColStr(scur, " MESSAGE " , & MESSAGE);
168 rc = SaCursorColInt(scur, " SEVERITY " , & SEVERITY);
169 rc = SaCursorColStr(scur, " EVENT_OID " , & EVENT_OID);
170 rc = SaCursorColStr(scur, " OV_OBJECTID " , & OV_OBJECTID);
171 rc = SaCursorColStr(scur, " PROTOCOL " , & PROTOCOL);
172 rc = SaCursorColStr(scur, " EVENT_TYPE " , & EVENT_TYPE);
173 rc = SaCursorColStr(scur, " IP_ADDRESS " , & IP_ADDRESS);
174 rc = SaCursorColInt(scur, " TRAPSOURCE " , & TRAPSOURCE);
175 rc = SaCursorColStr(scur, " TRAP_NAME " , & TRAP_NAME);
176 rc = SaCursorColInt(scur, " PID " , & PID);
177 rc = SaCursorColStr(scur, " FORWARD_ADDRESS " , & FORWARD_ADDRESS);
178 rc = SaCursorColStr(scur, " EVENT_SOURCE " , & EVENT_SOURCE);
179 rc = SaCursorColStr(scur, " EVENT_TIME " , & EVENT_TIME);
180 rc = SaCursorColInt(scur, " NUMBER_VARBINDS " , & NUMBER_VARBINDS);
181 rc = SaCursorColInt(scur, " IFNUMBER " , & IFNUMBER);
182 // -- bind variables from F1..F18
183
184
185 rc = SaCursorOpen(scur);
186 rc = SaCursorLike(scur, " EVENT_TIME " ,conf -> pattern.size());
187 rc = SaCursorDescending(scur, " EVENT_TIMESTAMP " );
188 rc = SaCursorSearch(scur);
189 while ( SaCursorNext(scur) == SA_RC_SUCC){
190 reload.TRAPSOURCE = TRAPSOURCE ;
191 reload.APPLICATION_ID = APPLICATION_ID ;
192 reload.PID = PID ;
193 reload.CATEGORY = CATEGORY ;
194 reload.SEVERITY = SEVERITY ;
195 reload.EVENT_TIMESTAMP = EVENT_TIMESTAMP;
196 reload.NUMBER_VARBINDS = NUMBER_VARBINDS;
197 reload.IFNUMBER = IFNUMBER ;
198 reload.TRAP_NAME = TRAP_NAME ;
199 reload.FORWARD_ADDRESS = FORWARD_ADDRESS;
200 reload.EVENT_SOURCE = EVENT_SOURCE ;
201 reload.EVENT_TIME = EVENT_TIME ;
202 reload.EVENT_UUID = EVENT_UUID ;
203 reload.NODENAME = NODENAME ;
204 reload.MESSAGE = MESSAGE ;
205 reload.EVENT_OID = EVENT_OID ;
206 reload.OV_OBJECTID = OV_OBJECTID ;
207 reload.PROTOCOL = PROTOCOL ;
208 reload.EVENT_TYPE = EVENT_TYPE ;
209 reload.IP_ADDRESS = IP_ADDRESS ;
210 write_file(conf,reload);
211 }
212 SaCursorFree(scur);
213 }
214
215 void filter_field(SysConf * conf){
216 FILE * file;
217 file = fopen( " ./dump.conf " , " r " );
218 char line[ 1024 ];
219 if ( ! file){
220 return ;
221 }
222 while ( fgets(line,sizeof(line),file) ){
223 conf -> fields.push_back(RPSTRING(line));
224 }
225
226 fclose(file);
227 }
228
229 int main( int argc, char * argv[]){
230 char c;
231 SysConf conf;
232 RPSTRING stime,etime;
233 SaRetT dbrc;
234 SaRetT rc;
235
236 SaCursorT * scur;
237 char buff[ 1024 ];
238 filter_field( & conf);
239
240 while ((c = getopt(argc, argv, " :h:p:U:P:f:l: " )) != - 1 ){
241
242 switch (c) {
243 case ' h ' :
244 conf.solid_host = optarg;
245 break ;
246 case ' p ' :
247 conf.solid_port = (unsigned short ) atoi(optarg);
248 break ;
249 case ' U ' :
250 conf.solid_user = optarg;
251 break ;
252 case ' P ' :
253 conf.solid_passwd = optarg;
254 break ;
255 case ' f ' :
256 conf.dump_file = optarg;
257 break ;
258 case ' l ' :
259 conf.pattern = optarg;
260 // printf("\n pattern:%s",optarg);
261 break ;
262 default :
263 usage();
264 exit( 1 );
265 }
266 }
267 if ( conf.pattern == "" ){
268 usage();
269 exit( 1 );
270 }
271
272
273 sprintf(buff, " tcp %s %d " ,conf.solid_host.c_str(),conf.solid_port);
274 dbscon = SaConnect(buff,( char * )conf.solid_user.c_str(),( char * )conf.solid_passwd.c_str());
275 if ( ! dbscon) {
276 printf( " \nconnect solid DB failed! " );
277 exit( 1 );
278 }
279 conf.file = fopen(conf.dump_file.c_str(), " w " );
280 if ( conf.file == NULL){
281 printf( " \n cann't open file:%s\n " ,conf.dump_file.c_str());
282 exit( 1 );
283 }
284 dump( & conf);
285 fclose(conf.file);
286 printf( " that's ok,to see dump file :%s\n " ,conf.dump_file.c_str());
287
288 return 0 ;
289 }
290
2 nnm_event_downup detail dump
3 2005.11.07 zhangbin created
4
5 */
6
7 #include < iostream >
8 #include < string >
9 #include < vector >
10 #include < stdio.h >
11 #include < unistd.h >
12
13 #include < c.h >
14 #include < sscapi.h >
15 #include < sa.h >
16
17
18 SaRetT dbrc;
19 SaConnectT * dbscon;
20
21
22 typedef std::string RPSTRING;
23 extern char * optarg;
24 extern int optind, optopt;
25
26
27 struct TableEventDownUp{
28 int TRAPSOURCE;
29 int APPLICATION_ID;
30 int PID;
31 int CATEGORY;
32 int SEVERITY;
33 int EVENT_TIMESTAMP;
34 int NUMBER_VARBINDS;
35 int IFNUMBER;
36 RPSTRING TRAP_NAME;
37 RPSTRING FORWARD_ADDRESS;
38 RPSTRING EVENT_SOURCE;
39 RPSTRING EVENT_TIME;
40 RPSTRING EVENT_UUID;
41 RPSTRING NODENAME;
42 RPSTRING MESSAGE;
43 RPSTRING EVENT_OID;
44 RPSTRING OV_OBJECTID;
45 RPSTRING PROTOCOL;
46 RPSTRING EVENT_TYPE;
47 RPSTRING IP_ADDRESS;
48 // char * fs[18];
49
50 };
51
52 typedef struct _sysconf{
53 RPSTRING solid_host;
54 unsigned short solid_port;
55 RPSTRING solid_user;
56 RPSTRING solid_passwd;
57 RPSTRING dump_file;
58 RPSTRING pattern;
59 FILE * file;
60 std::vector < RPSTRING > fields;
61 _sysconf(){
62 solid_host = " 127.0.0.1 " ;
63 solid_port = 2690 ;
64 solid_user = " ovdb " ;
65 solid_passwd = " ovdb " ;
66 dump_file = " ./event.dump " ;
67 pattern = "" ;
68
69
70 }
71 }SysConf;
72
73 void usage(){
74 char * msg = " usage: "
75 " \n-h solid db host "
76 " \n-p solid db port "
77 " \n-U solid user name "
78 " \n-P solid passwd "
79 " \n-f dump file "
80 " \n-l like pattern "
81 " \n " ;
82 printf( " %s " ,msg);
83 }
84
85
86
87
88 void INT_STRING(SysConf * conf, const char * field,RPSTRING & str, int intv){
89 char buff[ 32 ];
90 if ( std::find(conf -> fields.begin(),conf -> fields.end(),RPSTRING(field) + " \n " ) != conf -> fields.end()){
91 sprintf(buff, " \t%u " ,intv);
92 str += buff;
93 }
94 }
95
96 void STRING_STRING(SysConf * conf, const char * field,RPSTRING & str,RPSTRING str2){
97 if ( std::find(conf -> fields.begin(),conf -> fields.end(),RPSTRING(field) + " \n " ) != conf -> fields.end()){
98 str += " \t " + str2;
99 }
100 }
101
102 void write_file(SysConf * conf,TableEventDownUp row){
103 RPSTRING str = " " ;
104
105 INT_STRING(conf, " TRAPSOURCE " ,str,row.TRAPSOURCE);
106 INT_STRING(conf, " APPLICATION_ID " ,str,row.APPLICATION_ID);
107 INT_STRING(conf, " PID " ,str,row.PID);
108 INT_STRING(conf, " CATEGORY " ,str,row.CATEGORY);
109 INT_STRING(conf, " SEVERITY " ,str,row.SEVERITY);
110 INT_STRING(conf, " EVENT_TIMESTAMP " ,str,row.EVENT_TIMESTAMP);
111 INT_STRING(conf, " NUMBER_VARBINDS " ,str,row.NUMBER_VARBINDS);
112 INT_STRING(conf, " IFNUMBER " ,str,row.IFNUMBER);
113 STRING_STRING(conf, " TRAP_NAME " ,str,row.TRAP_NAME);
114 str += " \t " ;
115 STRING_STRING(conf, " FORWARD_ADDRESS " ,str,row.FORWARD_ADDRESS);
116 STRING_STRING(conf, " EVENT_SOURCE " ,str,row.EVENT_SOURCE);
117 STRING_STRING(conf, " EVENT_TIME " ,str,row.EVENT_TIME);
118 STRING_STRING(conf, " EVENT_UUID " ,str,row.EVENT_UUID);
119 STRING_STRING(conf, " NODENAME " ,str,row.NODENAME);
120 STRING_STRING(conf, " MESSAGE " ,str,row.MESSAGE);
121 STRING_STRING(conf, " EVENT_OID " ,str,row.EVENT_OID);
122 STRING_STRING(conf, " OV_OBJECTID " ,str,row.OV_OBJECTID);
123 STRING_STRING(conf, " PROTOCOL " ,str,row.PROTOCOL);
124 STRING_STRING(conf, " EVENT_TYPE " ,str,row.EVENT_TYPE);
125 STRING_STRING(conf, " IP_ADDRESS " ,str,row.IP_ADDRESS);
126 // -- rid of "LR"
127 std::replace(str.begin(),str.end(), ' \n ' , ' ' );
128 str += " \n " ;
129 // printf("\nrow==>%s",str.c_str());
130 // return;
131 fputs(str.c_str(),conf -> file);
132 }
133
134
135 void dump(SysConf * conf){
136 TableEventDownUp reload;
137 SaRetT rc;
138 SaCursorT * scur;
139 char * EVENT_UUID;
140 int EVENT_TIMESTAMP;
141 int CATEGORY;
142 char * NODENAME;
143 int APPLICATION_ID;
144 char * MESSAGE;
145 int SEVERITY;
146 char * EVENT_OID;
147 char * OV_OBJECTID;
148 char * PROTOCOL;
149 char * EVENT_TYPE;
150 char * IP_ADDRESS;
151 int TRAPSOURCE;
152 char * TRAP_NAME;
153 int PID;
154 char * FORWARD_ADDRESS;
155 char * EVENT_SOURCE;
156 char * EVENT_TIME;
157 int NUMBER_VARBINDS;
158 int IFNUMBER;
159
160 EVENT_TIME = ( char * )conf -> pattern.c_str();
161 scur = SaCursorCreate(dbscon, " NNM_EVENT_DOWNUP " );
162 rc = SaCursorColStr(scur, " EVENT_UUID " , & EVENT_UUID);
163 rc = SaCursorColInt(scur, " EVENT_TIMESTAMP " , & EVENT_TIMESTAMP);
164 rc = SaCursorColInt(scur, " CATEGORY " , & CATEGORY);
165 rc = SaCursorColStr(scur, " NODENAME " , & NODENAME);
166 rc = SaCursorColInt(scur, " APPLICATION_ID " , & APPLICATION_ID);
167 rc = SaCursorColStr(scur, " MESSAGE " , & MESSAGE);
168 rc = SaCursorColInt(scur, " SEVERITY " , & SEVERITY);
169 rc = SaCursorColStr(scur, " EVENT_OID " , & EVENT_OID);
170 rc = SaCursorColStr(scur, " OV_OBJECTID " , & OV_OBJECTID);
171 rc = SaCursorColStr(scur, " PROTOCOL " , & PROTOCOL);
172 rc = SaCursorColStr(scur, " EVENT_TYPE " , & EVENT_TYPE);
173 rc = SaCursorColStr(scur, " IP_ADDRESS " , & IP_ADDRESS);
174 rc = SaCursorColInt(scur, " TRAPSOURCE " , & TRAPSOURCE);
175 rc = SaCursorColStr(scur, " TRAP_NAME " , & TRAP_NAME);
176 rc = SaCursorColInt(scur, " PID " , & PID);
177 rc = SaCursorColStr(scur, " FORWARD_ADDRESS " , & FORWARD_ADDRESS);
178 rc = SaCursorColStr(scur, " EVENT_SOURCE " , & EVENT_SOURCE);
179 rc = SaCursorColStr(scur, " EVENT_TIME " , & EVENT_TIME);
180 rc = SaCursorColInt(scur, " NUMBER_VARBINDS " , & NUMBER_VARBINDS);
181 rc = SaCursorColInt(scur, " IFNUMBER " , & IFNUMBER);
182 // -- bind variables from F1..F18
183
184
185 rc = SaCursorOpen(scur);
186 rc = SaCursorLike(scur, " EVENT_TIME " ,conf -> pattern.size());
187 rc = SaCursorDescending(scur, " EVENT_TIMESTAMP " );
188 rc = SaCursorSearch(scur);
189 while ( SaCursorNext(scur) == SA_RC_SUCC){
190 reload.TRAPSOURCE = TRAPSOURCE ;
191 reload.APPLICATION_ID = APPLICATION_ID ;
192 reload.PID = PID ;
193 reload.CATEGORY = CATEGORY ;
194 reload.SEVERITY = SEVERITY ;
195 reload.EVENT_TIMESTAMP = EVENT_TIMESTAMP;
196 reload.NUMBER_VARBINDS = NUMBER_VARBINDS;
197 reload.IFNUMBER = IFNUMBER ;
198 reload.TRAP_NAME = TRAP_NAME ;
199 reload.FORWARD_ADDRESS = FORWARD_ADDRESS;
200 reload.EVENT_SOURCE = EVENT_SOURCE ;
201 reload.EVENT_TIME = EVENT_TIME ;
202 reload.EVENT_UUID = EVENT_UUID ;
203 reload.NODENAME = NODENAME ;
204 reload.MESSAGE = MESSAGE ;
205 reload.EVENT_OID = EVENT_OID ;
206 reload.OV_OBJECTID = OV_OBJECTID ;
207 reload.PROTOCOL = PROTOCOL ;
208 reload.EVENT_TYPE = EVENT_TYPE ;
209 reload.IP_ADDRESS = IP_ADDRESS ;
210 write_file(conf,reload);
211 }
212 SaCursorFree(scur);
213 }
214
215 void filter_field(SysConf * conf){
216 FILE * file;
217 file = fopen( " ./dump.conf " , " r " );
218 char line[ 1024 ];
219 if ( ! file){
220 return ;
221 }
222 while ( fgets(line,sizeof(line),file) ){
223 conf -> fields.push_back(RPSTRING(line));
224 }
225
226 fclose(file);
227 }
228
229 int main( int argc, char * argv[]){
230 char c;
231 SysConf conf;
232 RPSTRING stime,etime;
233 SaRetT dbrc;
234 SaRetT rc;
235
236 SaCursorT * scur;
237 char buff[ 1024 ];
238 filter_field( & conf);
239
240 while ((c = getopt(argc, argv, " :h:p:U:P:f:l: " )) != - 1 ){
241
242 switch (c) {
243 case ' h ' :
244 conf.solid_host = optarg;
245 break ;
246 case ' p ' :
247 conf.solid_port = (unsigned short ) atoi(optarg);
248 break ;
249 case ' U ' :
250 conf.solid_user = optarg;
251 break ;
252 case ' P ' :
253 conf.solid_passwd = optarg;
254 break ;
255 case ' f ' :
256 conf.dump_file = optarg;
257 break ;
258 case ' l ' :
259 conf.pattern = optarg;
260 // printf("\n pattern:%s",optarg);
261 break ;
262 default :
263 usage();
264 exit( 1 );
265 }
266 }
267 if ( conf.pattern == "" ){
268 usage();
269 exit( 1 );
270 }
271
272
273 sprintf(buff, " tcp %s %d " ,conf.solid_host.c_str(),conf.solid_port);
274 dbscon = SaConnect(buff,( char * )conf.solid_user.c_str(),( char * )conf.solid_passwd.c_str());
275 if ( ! dbscon) {
276 printf( " \nconnect solid DB failed! " );
277 exit( 1 );
278 }
279 conf.file = fopen(conf.dump_file.c_str(), " w " );
280 if ( conf.file == NULL){
281 printf( " \n cann't open file:%s\n " ,conf.dump_file.c_str());
282 exit( 1 );
283 }
284 dump( & conf);
285 fclose(conf.file);
286 printf( " that's ok,to see dump file :%s\n " ,conf.dump_file.c_str());
287
288 return 0 ;
289 }
290
1
2 #include " glodef.h "
3
4 #include < stdio.h >
5 #include < c.h >
6 #include < sscapi.h >
7 #include < sa.h >
8
9
10 SaRetT dbrc;
11 SaConnectT * dbscon;
12
13 int connect_solid(SysConf * conf){
14 char buff[ 128 ];
15 SaRetT rc;
16 // SaCursorT * scur;
17
18 sprintf(buff, " tcp %s %d " ,conf -> solid_host.c_str(),
19 conf -> solid_port);
20 dbscon = SaConnect(buff,( char * )conf -> solid_user.c_str(),
21 ( char * )conf -> solid_passwd.c_str());
22 if ( ! dbscon) {
23 return false ;
24 }
25 // -- check table
26 char * sql = " CREATE TABLE nnm_interfaces_extra\
27 (topo_id varchar NOT NULL PRIMARY KEY,\
28 ifspeed integer ,\
29 ifiextra1 integer ,\
30 ifiextra2 integer ,\
31 ifstrextra1 varchar ,\
32 ifstrextra2 varchar ,\
33 ifstrextra3 varchar \
34 ) " ;
35 rc = SaSQLExecDirect(dbscon,sql);
36 rc = SaTransCommit(dbscon);
37 if ( rc != SA_RC_SUCC){
38 rplog -> Print( 1 , " Create Table[NNM_INTERFACES_EXTRA] Failed! " );
39 return false ;
40 }
41
42
43 sql = " CREATE TABLE nnm_event_downup\
44 (event_uuid varchar NOT NULL PRIMARY KEY,\
45 event_timestamp integer NOT NULL,\
46 category integer NOT NULL, \
47 nodename varchar NOT NULL,\
48 application_id integer,\
49 message varchar,\
50 severity integer NOT NULL,\
51 event_oid varchar NOT NULL,\
52 ov_objectid varchar,\
53 protocol varchar,\
54 event_type varchar NOT NULL,\
55 ip_address varchar,\
56 trapsource integer,\
57 trap_name varchar,\
58 pid integer,\
59 forward_address varchar,\
60 event_source varchar,\
61 event_time varchar,\
62 number_varbinds integer,\
63 ifnumber integer\
64 ) " ;
65
66 rc = SaSQLExecDirect(dbscon,sql);
67 rc = SaTransCommit(dbscon);
68 if ( rc != SA_RC_SUCC){
69 rplog -> Print( 1 , " Create Table[NNM_EVENT_DOWNUP] Failed! " );
70 return false ;
71 }
72 return true ;
73 }
74
75 void free_solid(){
76
77 }
78
2 #include " glodef.h "
3
4 #include < stdio.h >
5 #include < c.h >
6 #include < sscapi.h >
7 #include < sa.h >
8
9
10 SaRetT dbrc;
11 SaConnectT * dbscon;
12
13 int connect_solid(SysConf * conf){
14 char buff[ 128 ];
15 SaRetT rc;
16 // SaCursorT * scur;
17
18 sprintf(buff, " tcp %s %d " ,conf -> solid_host.c_str(),
19 conf -> solid_port);
20 dbscon = SaConnect(buff,( char * )conf -> solid_user.c_str(),
21 ( char * )conf -> solid_passwd.c_str());
22 if ( ! dbscon) {
23 return false ;
24 }
25 // -- check table
26 char * sql = " CREATE TABLE nnm_interfaces_extra\
27 (topo_id varchar NOT NULL PRIMARY KEY,\
28 ifspeed integer ,\
29 ifiextra1 integer ,\
30 ifiextra2 integer ,\
31 ifstrextra1 varchar ,\
32 ifstrextra2 varchar ,\
33 ifstrextra3 varchar \
34 ) " ;
35 rc = SaSQLExecDirect(dbscon,sql);
36 rc = SaTransCommit(dbscon);
37 if ( rc != SA_RC_SUCC){
38 rplog -> Print( 1 , " Create Table[NNM_INTERFACES_EXTRA] Failed! " );
39 return false ;
40 }
41
42
43 sql = " CREATE TABLE nnm_event_downup\
44 (event_uuid varchar NOT NULL PRIMARY KEY,\
45 event_timestamp integer NOT NULL,\
46 category integer NOT NULL, \
47 nodename varchar NOT NULL,\
48 application_id integer,\
49 message varchar,\
50 severity integer NOT NULL,\
51 event_oid varchar NOT NULL,\
52 ov_objectid varchar,\
53 protocol varchar,\
54 event_type varchar NOT NULL,\
55 ip_address varchar,\
56 trapsource integer,\
57 trap_name varchar,\
58 pid integer,\
59 forward_address varchar,\
60 event_source varchar,\
61 event_time varchar,\
62 number_varbinds integer,\
63 ifnumber integer\
64 ) " ;
65
66 rc = SaSQLExecDirect(dbscon,sql);
67 rc = SaTransCommit(dbscon);
68 if ( rc != SA_RC_SUCC){
69 rplog -> Print( 1 , " Create Table[NNM_EVENT_DOWNUP] Failed! " );
70 return false ;
71 }
72 return true ;
73 }
74
75 void free_solid(){
76
77 }
78
1
2 #ifdef WIN32
3 #pragma warning(disable: 4786 )
4 #endif
5
6 #include " event.h "
7 #include " snmpprint.h "
8 #include " util.h "
9
10
11 #include < stdio.h >
12 #ifdef WIN32
13 #include < ctype.h >
14 #endif
15
16 #include < OV / ovw.h >
17
18 #include < stdio.h >
19 #include < c.h >
20 #include < sscapi.h >
21 #include < sa.h >
22
23
24
25 extern SaConnectT * dbscon;
26 OVsnmpVarBind * GetVarBindByIndex( int index,OVsnmpPdu * pdu);
27
28 void GrabEvent::pdu_process( int type, OVsnmpSession * session, OVsnmpPdu * pdu){
29 EventContext * ec;;
30 // char buff[1024*4];
31 // OVsnmpVarBind *var;
32 if ( ! pdu ){
33 return ;
34 }
35 switch (pdu -> specific_type ){
36 case EVENT_NODE_DOWN_ID:
37 case EVENT_NODE_UP_ID:
38 case EVENT_IF_DOWN_ID:
39 case EVENT_IF_UP_ID:
40 break ;
41 default :
42 return ;
43 }
44 rplog -> Print( 1 , " recive Event from NNM-ECS! %d " ,pdu -> specific_type);
45 /*
46 var = pdu->variables;
47 while(var){
48 sprint_by_type(buff,sizeof(buff),var,TYPE_VAL);
49 var = var->next_variable;
50 rplog->Print(1,"VarBind Detail:\t%s",buff);
51 } */
52
53
54 ec = new EventContext;
55 ec -> event = pdu -> specific_type;
56 ec -> pdu = OVsnmpCopyPdu(pdu);
57 ec -> check_time = _conf -> up_status_check_interval + time( 0 );
58
59 ec -> rid = DoUpdate(ec);
60 if ( pdu -> specific_type == EVENT_NODE_DOWN_ID ||
61 pdu -> specific_type == EVENT_IF_DOWN_ID){
62 _evctx_lock.Lock();
63
64 #ifdef _DEBUG
65 /*
66 for(int n=0;n<50;n++){
67 printf("\7");
68 Sleep(1);
69 } */
70 #endif
71 rplog -> Print( 1 , " %s Occur,Be Push Into Queue To Be Processed! " ,
72 pdu -> specific_type == EVENT_NODE_DOWN_ID ? " EVENT_NODE_DOWN_ID " : " EVENT_IF_DOWN_ID " );
73 _evctx_list.push_back(ec);
74 DumpDownEvent2File();
75 _evctx_lock.Unlock();
76 } else {
77 delete ec;
78 }
79 }
80
81 // --记录唯一的数据库编号,转存到本地文件
82 void GrabEvent::DumpDownEvent2File(){
83 FILE * fp;
84 EventContext_List::iterator itr;
85 EventContext * ec;
86
87 fp = fopen(FILE_RELOAD, " w " );
88 if ( ! fp ) return ;
89 rplog -> Print( 1 , " DumpList size:%d " ,_evctx_list.size());
90 for ( itr = _evctx_list.begin() ;itr != _evctx_list.end();itr ++ ){
91 ec = * itr;
92 // rplog->Print(1,"Dump Record ID:%s",ec->rid.c_str());
93 fwrite(ec -> rid.c_str(),ec -> rid.size(), 1 ,fp);
94 fwrite( " \n " ,strlen( " \n " ), 1 ,fp);
95 }
96 fclose(fp);
97 }
98
99 // --记录唯一的数据库编号,转存到本地文件
100 void GrabEvent::DumpFile2DownEvent(){
101 FILE * fp;
102 RPSTRING str;
103 // EventContext * ec;
104 char line[ 1024 ];
105 char * chars;
106
107 fp = fopen(FILE_RELOAD, " r " );
108 if ( ! fp ) return ;
109
110 while ( chars = fgets(line,sizeof(line),fp) ){
111 str = ValidString(chars);
112 LoadDBRecord(str);
113 // ec = new EventContext;
114 // ec->rid = str;
115 // _evctx_list.push_back(ec);
116 }
117 fclose(fp);
118 }
119
120
121 void GrabEvent::Run( void * ){
122 struct fd_set read_fds;
123 int count, num_snmp_fds;
124 struct timeval timeout;
125 rplog -> Print( 1 , " GrabEvent Thread Enter! " );
126 while (THREAD_CONTINUE_T) {
127 FD_ZERO( & read_fds);
128 num_snmp_fds = OVsnmpGetRetryInfo( & read_fds, & timeout );
129 if ( num_snmp_fds == 0 ) {
130 SLEEP_MSEC( 200 );
131 // fprintf(stderr, "testapp: No SNMP Sessions open. Goodbye.\n");
132 continue ;
133 }
134 // rplog->Print(1,"Block to Select");
135 count = select(num_snmp_fds, & read_fds, NULL, NULL, NULL);
136 // rplog->Print(1,"Something Arrived,Select it");
137 if (count < 0 ) {
138 perror( " select " );
139 // OVsnmpClose(session);
140 break ;
141 } else {
142 OVsnmpRead( & read_fds);
143 }
144 }
145 rplog -> Print( 1 , " GrabEvent Thread will exit " );
146 _update_thr -> Terminate();
147 _update_thr -> Wait();
148 rplog -> Print( 1 , " GrabEvent has been exited! " );
149 }
150
151 int GrabEvent::Connect(SysConf * conf){
152 const char * filter = " {ALL} .* " ;
153 _conf = conf;
154 // -- parse trapd.conf
155 _ep_list.insert(EventDetail_List::value_type(EVENT_NODE_DOWN_ID, & _tp_node_down));
156 _ep_list.insert(EventDetail_List::value_type(EVENT_NODE_UP_ID, & _tp_node_up));
157 _ep_list.insert(EventDetail_List::value_type(EVENT_IF_DOWN_ID, & _tp_if_down));
158 _ep_list.insert(EventDetail_List::value_type(EVENT_IF_UP_ID, & _tp_if_up));
159 LoadEventDetail(EVENT_NODE_DOWN_ID, & _tp_node_down);
160 LoadEventDetail(EVENT_NODE_UP_ID, & _tp_node_up);
161 LoadEventDetail(EVENT_IF_DOWN_ID, & _tp_if_down);
162 LoadEventDetail(EVENT_IF_UP_ID, & _tp_if_up);
163 // --加载目前未up的信息
164 // MessageBox(0,"begin to dumpfile2downevent",0,MB_OK);
165 DumpFile2DownEvent();
166 // MessageBox(0,"after dumpfile2downevent",0,MB_OK);
167
168
169 // --
170 session = OVsnmpEventOpen(conf -> nnm_host.c_str(),
171 " report-patch " ,
172 GrabThread::pdu_process_entry,
173 ( void * ) this ,
174 filter );
175 if (session == NULL) {
176 return false ;
177 }
178 _update_thr = new UpdateThread( this );
179 _update_thr -> Create();
180 NVThread::Create();
181 return true ;
182 }
183
184 // -- add on 05.08.10 13:00
185 void GrabEvent::LoadDBRecord(RPSTRING & rid){
186 // first start up
187 // TableEventDownUp * downup;
188 SaRetT rc;
189 SaCursorT * scur;
190 char * EVENT_UUID;
191 int EVENT_TIMESTAMP;
192 int CATEGORY;
193 char * NODENAME;
194 int APPLICATION_ID;
195 char * MESSAGE;
196 int SEVERITY;
197 char * EVENT_OID;
198 char * OV_OBJECTID;
199 char * PROTOCOL;
200 char * EVENT_TYPE;
201 char * IP_ADDRESS;
202 int TRAPSOURCE;
203 char * TRAP_NAME;
204 int PID;
205 char * FORWARD_ADDRESS;
206 char * EVENT_SOURCE;
207 char * EVENT_TIME;
208 int NUMBER_VARBINDS;
209 int IFNUMBER;
210
211 char * fs[ 18 ];
212 // char buff[128];
213 memset(fs, 0 ,sizeof( char * ) * 18 );
214 // --load downEvent from dump-file
215 EventContext * ec;
216 // EventContext_List::iterator itr;
217 // for( itr = _evctx_list.begin() ;itr!= _evctx_list.end();itr++){
218 // ec = *itr;
219
220 EVENT_UUID = ( char * )rid.c_str();
221
222 scur = SaCursorCreate(dbscon, " NNM_EVENT_DOWNUP " );
223 rc = SaCursorColStr(scur, " EVENT_UUID " , & EVENT_UUID);
224 rc = SaCursorColInt(scur, " EVENT_TIMESTAMP " , & EVENT_TIMESTAMP);
225 rc = SaCursorColInt(scur, " CATEGORY " , & CATEGORY);
226 rc = SaCursorColStr(scur, " NODENAME " , & NODENAME);
227 rc = SaCursorColInt(scur, " APPLICATION_ID " , & APPLICATION_ID);
228 rc = SaCursorColStr(scur, " MESSAGE " , & MESSAGE);
229 rc = SaCursorColInt(scur, " SEVERITY " , & SEVERITY);
230 rc = SaCursorColStr(scur, " EVENT_OID " , & EVENT_OID);
231 rc = SaCursorColStr(scur, " OV_OBJECTID " , & OV_OBJECTID);
232 rc = SaCursorColStr(scur, " PROTOCOL " , & PROTOCOL);
233 rc = SaCursorColStr(scur, " EVENT_TYPE " , & EVENT_TYPE);
234 rc = SaCursorColStr(scur, " IP_ADDRESS " , & IP_ADDRESS);
235 rc = SaCursorColInt(scur, " TRAPSOURCE " , & TRAPSOURCE);
236 rc = SaCursorColStr(scur, " TRAP_NAME " , & TRAP_NAME);
237 rc = SaCursorColInt(scur, " PID " , & PID);
238 rc = SaCursorColStr(scur, " FORWARD_ADDRESS " , & FORWARD_ADDRESS);
239 rc = SaCursorColStr(scur, " EVENT_SOURCE " , & EVENT_SOURCE);
240 rc = SaCursorColStr(scur, " EVENT_TIME " , & EVENT_TIME);
241 rc = SaCursorColInt(scur, " NUMBER_VARBINDS " , & NUMBER_VARBINDS);
242 rc = SaCursorColInt(scur, " IFNUMBER " , & IFNUMBER);
243 // -- bind variables from F1..F18
244 /*
245 for( int n =0; n< 18; n++){
246 sprintf(buff,"F%d",n+1);
247 rc = SaCursorColStr(scur,buff,&fs[n]);
248 } */
249
250
251 rc = SaCursorOpen(scur);
252 rc = SaCursorEqual(scur, " EVENT_UUID " );
253 rc = SaCursorSearch(scur);
254 if ( SaCursorNext(scur) != SA_RC_SUCC){
255 SaCursorFree(scur);
256 // continue;
257 return ;
258 }
259
260 {
261 ec = new EventContext;
262 ec -> rid = rid;
263 ec -> reload = new TableEventDownUp;
264 ec -> reload -> TRAPSOURCE = TRAPSOURCE ;
265 ec -> reload -> APPLICATION_ID = APPLICATION_ID ;
266 ec -> reload -> PID = PID ;
267 ec -> reload -> CATEGORY = CATEGORY ;
268 ec -> reload -> SEVERITY = SEVERITY ;
269 ec -> reload -> EVENT_TIMESTAMP = EVENT_TIMESTAMP;
270 ec -> reload -> NUMBER_VARBINDS = NUMBER_VARBINDS;
271 ec -> reload -> IFNUMBER = IFNUMBER ;
272 ec -> reload -> TRAP_NAME = TRAP_NAME ;
273 ec -> reload -> FORWARD_ADDRESS = FORWARD_ADDRESS;
274 ec -> reload -> EVENT_SOURCE = EVENT_SOURCE ;
275 ec -> reload -> EVENT_TIME = EVENT_TIME ;
276 ec -> reload -> EVENT_UUID = EVENT_UUID ;
277 ec -> reload -> NODENAME = NODENAME ;
278 ec -> reload -> MESSAGE = MESSAGE ;
279 ec -> reload -> EVENT_OID = EVENT_OID ;
280 ec -> reload -> OV_OBJECTID = OV_OBJECTID ;
281 ec -> reload -> PROTOCOL = PROTOCOL ;
282 ec -> reload -> EVENT_TYPE = EVENT_TYPE ;
283 ec -> reload -> IP_ADDRESS = IP_ADDRESS ;
284 // -=- load fields from F1..F18,data copy deeply
285 /*
286 for(int n=0;n<18;n++){
287 strcpy(ec->reload->fs[n],fs[n]);
288 } */
289 // -- have read out field's values
290 }
291 SaCursorFree(scur);
292 if ( strcmp(EVENT_OID, " 1.3.6.1.4.1.11.2.17.1.0.58916865 " ) == 0 ){
293 ec -> event = EVENT_NODE_DOWN_ID;
294 } else if ( strcmp(EVENT_OID, " 1.3.6.1.4.1.11.2.17.1.0.58916867 " ) == 0 ){
295 ec -> event = EVENT_IF_DOWN_ID;
296 } else {
297 delete ec;
298 return ;
299 }
300
301 ec -> pdu = NULL;
302 ec -> check_time = _conf -> up_status_check_interval + time( 0 );
303 _evctx_lock.Lock();
304 _evctx_list.push_back(ec);
305 _evctx_lock.Unlock();
306 // }
307 }
308
309 GrabEvent::GrabEvent(){
310 _starttime = time( 0 );
311 _evuuid = 0 ;
312 }
313
314
315
316 RPSTRING GrabEvent::DoUpdate(EventContext * ec){
317 SaRetT rc ;
318 SaCursorT * scur = NULL;
319 RPSTRING ret;
320 char * EVENT_UUID;
321 int EVENT_TIMESTAMP;
322 int CATEGORY;
323 char * NODENAME;
324 int APPLICATION_ID;
325 char * MESSAGE;
326 int SEVERITY;
327 char * EVENT_OID;
328 char * OV_OBJECTID;
329 char * PROTOCOL;
330 char * EVENT_TYPE;
331 char * IP_ADDRESS;
332 int TRAPSOURCE;
333 char * TRAP_NAME;
334 int PID;
335 char * FORWARD_ADDRESS;
336 char * EVENT_SOURCE;
337 char * EVENT_TIME;
338 int NUMBER_VARBINDS;
339 int IFNUMBER;
340
341 scur = SaCursorCreate(dbscon, " NNM_EVENT_DOWNUP " );
342
343 rc = SaCursorColStr(scur, " EVENT_UUID " , & EVENT_UUID);
344 rc = SaCursorColInt(scur, " EVENT_TIMESTAMP " , & EVENT_TIMESTAMP);
345 rc = SaCursorColInt(scur, " CATEGORY " , & CATEGORY);
346 rc = SaCursorColStr(scur, " NODENAME " , & NODENAME);
347 rc = SaCursorColInt(scur, " APPLICATION_ID " , & APPLICATION_ID);
348 rc = SaCursorColStr(scur, " MESSAGE " , & MESSAGE);
349 rc = SaCursorColInt(scur, " SEVERITY " , & SEVERITY);
350 rc = SaCursorColStr(scur, " EVENT_OID " , & EVENT_OID);
351 rc = SaCursorColStr(scur, " OV_OBJECTID " , & OV_OBJECTID);
352 rc = SaCursorColStr(scur, " PROTOCOL " , & PROTOCOL);
353 rc = SaCursorColStr(scur, " EVENT_TYPE " , & EVENT_TYPE);
354 rc = SaCursorColStr(scur, " IP_ADDRESS " , & IP_ADDRESS);
355 rc = SaCursorColInt(scur, " TRAPSOURCE " , & TRAPSOURCE);
356 rc = SaCursorColStr(scur, " TRAP_NAME " , & TRAP_NAME);
357 rc = SaCursorColInt(scur, " PID " , & PID);
358 rc = SaCursorColStr(scur, " FORWARD_ADDRESS " , & FORWARD_ADDRESS);
359 rc = SaCursorColStr(scur, " EVENT_SOURCE " , & EVENT_SOURCE);
360 rc = SaCursorColStr(scur, " EVENT_TIME " , & EVENT_TIME);
361 rc = SaCursorColInt(scur, " NUMBER_VARBINDS " , & NUMBER_VARBINDS);
362 rc = SaCursorColInt(scur, " IFNUMBER " , & IFNUMBER);
363
364
365 rc = SaCursorOpen(scur);
366 if ( rc != SA_RC_SUCC){
367 rplog -> Print( 1 , " ReloadSpeedContext::SaCursorOpen Failed! " );
368 return 0 ;
369 }
370 EVENT_TIMESTAMP = GetEventTimestamp(ec);
371 CATEGORY = GetCategory(ec);
372 APPLICATION_ID = GetAppID(ec);
373 SEVERITY = GetSeverity(ec);
374 TRAPSOURCE = GetTrapSource(ec);
375 PID = GetPID(ec);
376 NUMBER_VARBINDS = GetNumVars(ec);
377 IFNUMBER = GetIfNumber(ec);
378 EVENT_UUID = new char [FIELD_STRING_SIZE];
379 strcpy(EVENT_UUID,GetEventUUID().c_str());
380 NODENAME = new char [FIELD_STRING_SIZE];
381 strcpy(NODENAME ,GetNodeName(ec).c_str());
382 MESSAGE = new char [FIELD_STRING_SIZE];
383 strcpy(MESSAGE,GetMessage(ec).c_str());
384 EVENT_OID = new char [FIELD_STRING_SIZE];
385 strcpy(EVENT_OID,GetEvent_OID(ec).c_str());
386 OV_OBJECTID = new char [FIELD_STRING_SIZE];
387 strcpy(OV_OBJECTID,GetOVObjectID(ec).c_str());
388 PROTOCOL = new char [FIELD_STRING_SIZE];
389 strcpy(PROTOCOL,GetProtocal(ec).c_str());
390 EVENT_TYPE = new char [FIELD_STRING_SIZE];
391 strcpy(EVENT_TYPE,GetEventType(ec).c_str());
392 IP_ADDRESS = new char [FIELD_STRING_SIZE];
393 strcpy(IP_ADDRESS,GetIpAddress(ec).c_str());
394 TRAP_NAME = new char [FIELD_STRING_SIZE];
395 strcpy(TRAP_NAME,GetTrapName(ec).c_str());
396 FORWARD_ADDRESS = new char [FIELD_STRING_SIZE];
397 strcpy(FORWARD_ADDRESS,GetForwardAddress(ec).c_str());
398 EVENT_SOURCE = new char [FIELD_STRING_SIZE];
399 strcpy(EVENT_SOURCE,GetEventSource(ec).c_str());
400 EVENT_TIME = new char [FIELD_STRING_SIZE];
401 strcpy(EVENT_TIME,GetEventTimeString(ec).c_str());
402
403 rc = SaCursorInsert(scur);
404 rc = SaTransCommit(dbscon);
405 SaCursorFree(scur);
406
407 ret = EVENT_UUID;
408
409 delete[] EVENT_UUID;
410 delete[] NODENAME;
411 delete[] MESSAGE;
412 delete[] EVENT_OID;
413 delete[] OV_OBJECTID;
414 delete[] PROTOCOL;
415 delete[] EVENT_TYPE;
416 delete[] IP_ADDRESS;
417 delete[] TRAP_NAME;
418 delete[] FORWARD_ADDRESS;
419 delete[] EVENT_SOURCE;
420 delete[] EVENT_TIME;
421 /*
422 //--add on 2005.08.10
423 if( ec->reload == NULL){
424 //--写入pdu所有参数进表 F1..F18
425 PduVars2DB(ec,ret.c_str());
426 } */
427
428 return ret;
429 }
430
431 int GrabEvent::PduVars2DB(EventContext * ec, const char * rid){
432 char * fs[ 18 ];
433 char buff[ 128 ];
434 int n;
435
436 OVsnmpVarBind * var;
437 memset(fs, 0 ,sizeof( char * ) * 18 );
438
439
440 SaRetT rc;
441 SaCursorT * scur;
442
443 char * pid;
444 pid = ( char * )rid;
445 scur = SaCursorCreate(dbscon, " NNM_EVENT_DOWNUP " );
446 rc = SaCursorColStr(scur, " EVENT_UUID " , & pid);
447
448 for ( n = 0 ;n < 18 ;n ++ ){
449 sprintf(buff, " F%d " ,n + 1 );
450 rc = SaCursorColStr(scur,buff, & fs[n]);
451 }
452 rc = SaCursorOpen(scur);
453 rc = SaCursorEqual(scur, " EVENT_UUID " );
454 rc = SaCursorSearch(scur);
455 if ( SaCursorNext(scur) != SA_RC_SUCC){
456 SaCursorFree(scur);
457 return false ;
458 }
459 for ( n = 0 ;n < 18 ;n ++ ){
460 fs[n] = new char [FIELD_STRING_SIZE];
461 memset(fs[n], 0 ,FIELD_STRING_SIZE);
462 }
463 n = 0 ;
464 var = ec -> pdu -> variables;
465 while ( var ){
466 sprint_by_type(fs[n],FIELD_STRING_SIZE,var,VAL_ONLY);
467 n ++ ;
468 var = var -> next_variable;
469 }
470 rc = SaCursorUpdate(scur);
471
472 for ( n = 0 ;n < 18 ;n ++ ){
473 delete[] fs[n];
474 }
475 return true ;
476 }
477
478 // --明天做
479 void GrabEvent::GetCategorySeverity(EventDetail * ed, char * line){
480 if ( strstr(line, " LOGONLY " )){
481 ed -> cat.id = 1 ;
482 } else if ( strstr(line, " Error Alarms " )){
483 ed -> cat.id = 2 ;
484 } else if ( strstr(line, " Threshold Alarms " )){
485 ed -> cat.id = 3 ;
486 } else if ( strstr(line, " Status Alarms " )){
487 ed -> cat.id = 4 ;
488 } else if ( strstr(line, " Configuration Alarms " )){
489 ed -> cat.id = 5 ;
490 } else if ( strstr(line, " Application Alert Alarms " )){
491 ed -> cat.id = 6 ;
492 }
493 // ----
494 if ( strstr(line, " Normal " ) ){
495 ed -> seve.id = 1 ;
496 } else if ( strstr(line, " Warning " ) ){
497 ed -> seve.id = 2 ;
498 } else if ( strstr(line, " Minor " ) ){
499 ed -> seve.id = 3 ;
500 } else if ( strstr(line, " Major " ) ){
501 ed -> seve.id = 4 ;
502 } else if ( strstr(line, " Critical " ) ){
503 ed -> seve.id = 5 ;
504 }
505 }
506
507 void GrabEvent::LoadEventDetail( int ev,EventDetail * ed){
508 FILE * fp;
509 char line[ 1024 ];
510 char buff[ 1024 ];
511 char * occur;
512 char * s; // ,*e;
513 char * p = 0 ;
514 if ( _conf -> trapd == "" ){
515 printf( " \n error: read trapd.conf failed! " );
516 exit( 2 );
517 }
518 fp = fopen(_conf -> trapd.c_str(), " r " );
519 if ( ! fp ){
520 printf( " \n error: read trapd.conf fopen failed! %s " ,_conf -> trapd.c_str());
521 exit( 2 );
522 }
523 while ( occur = fgets(line,sizeof(line),fp)){
524 sprintf(buff, " %u " ,ev);
525 if ( p = strstr(line,buff) ){
526 // -- deal with first line,grab category and severity
527 GetCategorySeverity(ed,line);
528 // -- get next line
529 occur = fgets(line,sizeof(line),fp);
530 if ( ! occur){
531 break ;
532 }
533 // -- to parse parameters
534
535 EventFormatSplitedPart * esp;
536 if ( strstr(occur, " FORMAT " ) ){
537 occur += strlen( " FORMAT " ) + 1 ;
538 }
539 for (s = occur; * s != ' \0 ' ;s ++ ) {
540 if ( * s == ' $ ' ){
541 p = s;
542 continue ;
543 }
544 if ( p && ! isdigit( * s) && (s - p) > 1 ){
545 {
546 if ( p - occur > 0 ){
547 esp = new EventFormatSplitedPart;
548 esp -> type = EventFormatSplitedPart::PROMPT;
549 memset(buff, 0 ,p - occur + 1 );
550 memcpy(buff,occur,p - occur);
551 esp -> val = buff;
552 ed -> list.push_back(esp);
553 }
554 }
555 {
556 memset(buff, 0 ,s - p);
557 memcpy(buff,p + 1 ,s - p - 1 );
558 esp = new EventFormatSplitedPart;
559 esp -> type = EventFormatSplitedPart::PARAMETER;
560 esp -> val = buff;
561 ed -> list.push_back(esp);
562
563 p = 0 ;
564 if ( * s == ' $ ' ){
565 s -- ;
566 }
567 }
568 occur = s;
569 } // -- end if
570 } // -- end for
571 if ( (strlen(line) + line) > occur){
572 esp = new EventFormatSplitedPart;
573 esp -> type = EventFormatSplitedPart::PROMPT;
574 esp -> val = occur;
575 ed -> list.push_back(esp);
576 }
577 }
578 }
579 fclose(fp);
580 }
581
582 RPSTRING GrabEvent::GetEventUUID(){
583 char buff[ 512 ];
584 sprintf(buff, " %x-%x-000d-71da-0f4e-c0a80e890000 " ,_starttime, ++ _evuuid);
585 return RPSTRING(buff);
586 }
587
588 int GrabEvent::GetEventTimestamp(EventContext * ec){
589 int ret;
590 // OVsnmpVarBind* var;
591 // char buff[20];
592 ret = 0 ;
593 /*
594 memset(buff,0,sizeof(buff));
595 var = GetVarBindByIndex(5,ec->pdu);
596 if( var->type == ASN_OCTET_STR){
597 sprintf(buff,(char*)var->val.string,var->val_len);
598 ret = atoi(buff);
599 } */
600 ret = time( 0 );
601 return ret;
602 }
603
604 int GrabEvent::GetCategory(EventContext * ec){
605 int ret;
606 ret = 0 ;
607 EventDetail * ed = _ep_list[ec -> event];
608 if ( ed ) ret = ed -> cat.id;
609 return ret;
610 }
611 int GrabEvent::GetAppID(EventContext * ec){
612 int ret = 0 ;
613 OVsnmpVarBind * var;
614 if ( ec -> reload ){
615 return ec -> reload -> APPLICATION_ID;
616 }
617 var = GetVarBindByIndex( 1 ,ec -> pdu);
618 if ( var )
619 ret = * var -> val.integer;
620 return ret;
621 }
622
623 int GrabEvent::GetSeverity(EventContext * ec){
624 int ret = 0 ;
625 EventDetail * ed = _ep_list[ec -> event];
626 if ( ed ) ret = ed -> seve.id;
627 return ret;
628 }
629 int GrabEvent::GetTrapSource(EventContext * ec){
630 int ret = 0 ;
631 return ret;
632 }
633 int GrabEvent::GetPID(EventContext * ec){
634 int ret = 0 ;
635 return ret;
636 }
637
638 int GrabEvent::GetNumVars(EventContext * ec){
639 int ret = 0 ;
640 OVsnmpVarBind * var;
641 int n;
642 n = 0 ;
643 if ( ec -> reload){
644 return ec -> reload -> NUMBER_VARBINDS;
645 }
646 var = ec -> pdu -> variables;
647 while ( var ){
648 n ++ ;
649 var = var -> next_variable;
650 }
651 ret = n;
652 return ret;
653 }
654
655 int GrabEvent::GetIfNumber(EventContext * ec){
656 OVwFieldId fid ;
657 OVwObjectId oid ;
658 RPSTRING ip;
659 OVsnmpVarBind * var;
660 OVwFieldValue * fv;
661 char buff[ 128 ];
662
663 int ret = 0 ;
664
665 if ( ec -> reload){
666 return ec -> reload -> IFNUMBER;
667 }
668
669 if ( ec -> event == EVENT_NODE_DOWN_ID || ec -> event == EVENT_NODE_UP_ID){
670 ret = 0 ;
671 } else if ( ec -> event == EVENT_IF_DOWN_ID || ec -> event == EVENT_IF_UP_ID){
672 var = GetVarBindByIndex( 6 ,ec -> pdu);
673 if ( ! var ){
674 return false ;
675 }
676 if ( var -> type == ASN_OCTET_STR){
677 buff[var -> val_len] = ' \0 ' ;
678 memcpy(buff,var -> val.string,var -> val_len);
679 oid = atoi(buff);
680 } else if (var -> type == ASN_INTEGER){
681 oid = * var -> val.integer;
682 } else {
683 return ret;
684 }
685 fid = OVwDbFieldNameToFieldId( " SNMP ifIndex " );
686 fv = OVwDbGetFieldValue(oid,fid);
687 if ( fv) {
688 ret = fv -> un.int_val;
689 }
690 }
691 return ret;
692 }
693
694 RPSTRING GrabEvent::GetNodeName(EventContext * ec){
695 RPSTRING ret = " Unknow NodeName " ;
696 char buf[ 2048 ];
697 int buflen = sizeof(buf);
698 OVsnmpVarBind * var;
699
700 if ( ec -> reload){
701 return ec -> reload -> NODENAME;
702 }
703 var = GetVarBindByIndex( 2 ,ec -> pdu);
704 if ( var )
705 sprint_by_type(buf, buflen, var, TYPE_VAL);
706 ret = buf;
707 return ret;
708 }
709
710 RPSTRING GrabEvent::GetMessage(EventContext * ec){
711 RPSTRING ret = "" ;
712 char buf[ 2048 ];
713 OVsnmpVarBind * var;
714 int idx;
715 int n;
716 int buflen = sizeof(buf);
717 EventDetail * ed = _ep_list[ec -> event];
718 if ( ec -> reload ){
719 return ec -> reload -> MESSAGE;
720 }
721
722 for (n = 0 ;n < ed -> list.size();n ++ ){
723 if ( ed -> list.at(n) -> type == EventFormatSplitedPart::PROMPT){
724 ret << ed -> list.at(n) -> val << " " ;
725 continue ;
726 }
727 idx = atoi(ed -> list.at(n) -> val.c_str());
728 var = GetVarBindByIndex(idx,ec -> pdu);
729 if ( var ){
730 sprint_by_type(buf,buflen,var,VAL_ONLY);
731 ret << RPSTRING( " " ) << buf;
732 }
733 }
734 return ret;
735 }
736
737 RPSTRING GrabEvent::GetEvent_OID(EventContext * ec){
738 RPSTRING ret;
739
740 switch (ec -> event){
741 case EVENT_NODE_DOWN_ID:
742 ret = " 1.3.6.1.4.1.11.2.17.1.0.58916865 " ;
743 break ;
744 case EVENT_NODE_UP_ID:
745 ret = " 1.3.6.1.4.1.11.2.17.1.0.58916864 " ;
746 break ;
747 case EVENT_IF_DOWN_ID:
748 ret = " 1.3.6.1.4.1.11.2.17.1.0.58916867 " ;
749 break ;
750
751 case EVENT_IF_UP_ID:
752 ret = " 1.3.6.1.4.1.11.2.17.1.0.58916866 " ;
753 }
754 return ret;
755 }
756
757 RPSTRING GrabEvent::GetOVObjectID(EventContext * ec){
758 RPSTRING ret = " 0 " ;
759 char buff[ 1024 ];
760 OVsnmpVarBind * var;
761 if ( ec -> reload){
762 return ec -> reload -> OV_OBJECTID;
763 }
764
765 if ( ec -> event == EVENT_NODE_DOWN_ID || ec -> event == EVENT_NODE_UP_ID){
766 var = GetVarBindByIndex( 3 ,ec -> pdu);
767 } else if ( ec -> event == EVENT_IF_DOWN_ID || ec -> event == EVENT_IF_UP_ID){
768 var = GetVarBindByIndex( 6 ,ec -> pdu);
769 }
770 if ( ! var ){
771 return ret;
772 }
773
774 if ( var -> type == ASN_OCTET_STR){
775 buff[var -> val_len] = ' \0 ' ;
776 memcpy(buff,var -> val.string,var -> val_len);
777 ret = buff;
778 }
779 return ret;
780 }
781
782 RPSTRING GrabEvent::GetProtocal(EventContext * ec){
783 RPSTRING ret;
784 ret = " Canonical(generic)-event " ;
785 return ret;
786 }
787
788 RPSTRING GrabEvent::GetEventType(EventContext * ec){
789 RPSTRING ret;
790 ret << " . " << GetEvent_OID(ec);
791 return ret;
792 }
793
794 RPSTRING GrabEvent::GetIpAddress(EventContext * ec){
795 RPSTRING ret;
796 in_addr in;
797 if ( ec -> reload){
798 return ec -> reload -> IP_ADDRESS;
799 }
800 in.s_addr = ec -> pdu -> address.sin_addr.s_addr;
801 ret = inet_ntoa(in);
802 return ret;
803 }
804
805 RPSTRING GrabEvent::GetTrapName(EventContext * ec){
806 RPSTRING ret = "" ;
807 switch (ec -> event){
808 case EVENT_NODE_DOWN_ID:
809 ret = " OV_Node_Down " ;
810 break ;
811 case EVENT_NODE_UP_ID:
812 ret = " OV_Node_Up " ;
813 break ;
814 case EVENT_IF_DOWN_ID:
815 ret = " OV_IF_Down " ;
816 break ;
817 case EVENT_IF_UP_ID:
818 ret = " OV_IF_Up " ;
819 break ;
820 }
821 return ret;
822 }
823
824 RPSTRING GrabEvent::GetForwardAddress(EventContext * ec){
825 RPSTRING ret;
826 ret = " 0.0.0.0 " ;
827 return ret;
828 }
829
830 RPSTRING GrabEvent::GetEventSource(EventContext * ec){
831 RPSTRING ret;
832 ret = " IP " ;
833 return ret;
834 }
835
836 RPSTRING GrabEvent::GetEventTimeString(EventContext * ec){
837 RPSTRING ret;
838 unsigned int t = GetEventTimestamp(ec);
839 char buff[ 100 ];
840 tm * ptm;
841 ptm = localtime((time_t * ) & t);
842 sprintf(buff, " %04d-%02d-%02d %02d:%02d:%02d " ,ptm -> tm_year + 1900 ,ptm -> tm_mon + 1 ,ptm -> tm_mday,ptm -> tm_hour,ptm -> tm_min,ptm -> tm_sec);
843 ret = buff;
844 return ret;
845 }
846
847
848 int GrabEvent::IsUp(EventContext * ec){
849 // -- to do check ,monday work,eyes litte tired
850 // -- invoke ovw functions
851
852 OVwFieldId fid ;
853 OVwObjectId oid ;
854 RPSTRING ip;
855 OVsnmpVarBind * var;
856 OVwFieldValue * fv;
857 char buff[ 128 ];
858
859 if ( ec -> reload){ // --历史加载数据
860 oid = atoi(ec -> reload -> OV_OBJECTID.c_str());
861 } else {
862 if ( ec -> event == EVENT_NODE_DOWN_ID){
863 var = GetVarBindByIndex( 3 ,ec -> pdu);
864 } else if ( ec -> event == EVENT_IF_DOWN_ID){
865 var = GetVarBindByIndex( 6 ,ec -> pdu);
866 }
867 if ( ! var ){
868 return false ;
869 }
870 buff[var -> val_len] = ' \0 ' ;
871 memcpy(buff,var -> val.string,var -> val_len);
872 oid = atoi(buff);
873 }
874 fid = OVwDbFieldNameToFieldId( " IP Status " );
875 fv = OVwDbGetFieldValue(oid,fid);
876 if ( fv ){
877 if ( fv -> un.enum_val != 0 && fv -> un.enum_val != 4 ){
878 rplog -> Print( 1 , " [%s]OpenView ObjectID [%d] status is UP! " ,
879 ec -> event == EVENT_NODE_DOWN_ID ? " EVENT_NODE_DOWN_ID " : " EVENT_IF_DOWN_ID " ,
880 oid);
881 return true ;
882 }
883 }
884 /*
885 var = GetVarBindByIndex(2,ec->pdu){
886 if( var->type == ASN_OCTET_STR){
887 buff[var->val_len] ='\0';
888 memcpy(buff,var->val.string,var->val_len);
889 fid = OVwDbFieldNameToFieldId("IP Status");
890 oid = OVwDbHostnameToObjectId(buff);
891 fv = OVwDbGetFieldValue(oid,fid);
892 rplog->Print(1,"Test Host Node[%s] Status ",buff);
893 if( fv ){
894 if( fv->un.enum_val != 0 && fv->un.enum_val!=4){
895 rplog->Print(1,"Node[%s] status is UP!",buff);
896 return true;
897 }
898 }
899 } */
900 return false ;
901 }
902
903 void GrabEvent::Close(){
904 _update_thr -> Terminate();
905 _update_thr -> Wait();
906 rplog -> Print( 1 , " terminated upthread ! " );
907 OVsnmpClose(session);
908 Terminate();
909 Wait();
910 }
911
912 RPSTRING GetDetectObjectName(EventContext * ec){
913 OVsnmpVarBind * var;
914 RPSTRING ret;
915 char buff[ 1024 ];
916 ret = "" ;
917 if ( ec -> reload ){
918 ret << ec -> reload -> NODENAME << " :IF=> " << ec -> reload -> IP_ADDRESS;
919 return ret;
920 }
921 var = GetVarBindByIndex( 2 ,ec -> pdu);
922 if ( ! var){
923 return ret;
924 }
925
926 buff[var -> val_len] = ' \0 ' ;
927 memcpy(buff,var -> val.string,var -> val_len);
928 ret = buff;
929
930 if ( ec -> event == EVENT_NODE_DOWN_ID){
931 ;
932 } else {
933 var = GetVarBindByIndex( 7 ,ec -> pdu);
934 if ( ! var || var -> type != ASN_OCTET_STR){
935 ;
936 } else {
937 buff[var -> val_len] = ' \0 ' ;
938 memcpy(buff,var -> val.string,var -> val_len);
939 ret << " -(IF) " ;
940 ret << buff;
941 }
942 }
943 return ret ;
944 }
945 //////////////////////////////////////////////////////////////////////////
946 // status detect thread
947 void UpdateThread::Run( void * ){
948 EventContext_List::iterator itr;
949 while ( THREAD_CONTINUE_T ){
950 SLEEP_MSEC( 100 );
951 _master -> _evctx_lock.Lock();
952 for ( itr = _master -> _evctx_list.begin() ;itr != _master -> _evctx_list.end();
953 itr ++ ){
954 EventContext * ec;
955 ec = * itr;
956
957 time_t now = time( 0 );
958 if ( now >= ec -> check_time){ // should be checked
959 // -- check status
960 // rplog->Print(1,"准备检测状态,事件源:%s,对象:%s",ec->event==EVENT_NODE_DOWN_ID?
961 // "EVENT_NODE_DOWN_ID":"EVENT_IF_DOWN_ID",GetDetectObjectName(ec).c_str());
962 if ( _master -> IsUp(ec) ){
963 // modify event's status
964 if ( ec -> event == EVENT_NODE_DOWN_ID){
965 ec -> event = EVENT_NODE_UP_ID;
966 }
967 if ( ec -> event == EVENT_IF_DOWN_ID){
968 ec -> event = EVENT_IF_UP_ID;
969 }
970 // time will be updated in GetEventTimeStamp()
971 // rplog->Print(1,"设备[%s]已转化为UP状态,开始更新数据库",GetDetectObjectName(ec).c_str());
972 ec -> shift = true ;
973 _master -> DoUpdate(ec);
974 _master -> _evctx_list.erase(itr);
975 delete ec;
976 _master -> DumpDownEvent2File(); // rewrite event file for next-load
977 rplog -> Print( 1 , " 残留轮训队列对象数:%d " ,_master -> _evctx_list.size());
978 break ; // haha : lazy to jump out,wait next
979 } else {
980 // rplog->Print(1,"设备[%s]没有转化为UP状态,继续等待超时",GetDetectObjectName(ec).c_str());
981 ec -> check_time = now + _master -> _conf -> up_status_check_interval;
982 }
983 }
984 }
985 _master -> _evctx_lock.Unlock();
986 }
987 rplog -> Print( 1 , " UpThread Exiting! " );
988 }
989
990 //////////////////////////////////////////////////////////////////////////
991
992 void GrabThread::pdu_process_entry( int type, OVsnmpSession * session, OVsnmpPdu * pdu, void * data){
993 GrabThread * gt = (GrabThread * ) data;
994 gt -> pdu_process(type, session, pdu);
995 }
996
997
998
2 #ifdef WIN32
3 #pragma warning(disable: 4786 )
4 #endif
5
6 #include " event.h "
7 #include " snmpprint.h "
8 #include " util.h "
9
10
11 #include < stdio.h >
12 #ifdef WIN32
13 #include < ctype.h >
14 #endif
15
16 #include < OV / ovw.h >
17
18 #include < stdio.h >
19 #include < c.h >
20 #include < sscapi.h >
21 #include < sa.h >
22
23
24
25 extern SaConnectT * dbscon;
26 OVsnmpVarBind * GetVarBindByIndex( int index,OVsnmpPdu * pdu);
27
28 void GrabEvent::pdu_process( int type, OVsnmpSession * session, OVsnmpPdu * pdu){
29 EventContext * ec;;
30 // char buff[1024*4];
31 // OVsnmpVarBind *var;
32 if ( ! pdu ){
33 return ;
34 }
35 switch (pdu -> specific_type ){
36 case EVENT_NODE_DOWN_ID:
37 case EVENT_NODE_UP_ID:
38 case EVENT_IF_DOWN_ID:
39 case EVENT_IF_UP_ID:
40 break ;
41 default :
42 return ;
43 }
44 rplog -> Print( 1 , " recive Event from NNM-ECS! %d " ,pdu -> specific_type);
45 /*
46 var = pdu->variables;
47 while(var){
48 sprint_by_type(buff,sizeof(buff),var,TYPE_VAL);
49 var = var->next_variable;
50 rplog->Print(1,"VarBind Detail:\t%s",buff);
51 } */
52
53
54 ec = new EventContext;
55 ec -> event = pdu -> specific_type;
56 ec -> pdu = OVsnmpCopyPdu(pdu);
57 ec -> check_time = _conf -> up_status_check_interval + time( 0 );
58
59 ec -> rid = DoUpdate(ec);
60 if ( pdu -> specific_type == EVENT_NODE_DOWN_ID ||
61 pdu -> specific_type == EVENT_IF_DOWN_ID){
62 _evctx_lock.Lock();
63
64 #ifdef _DEBUG
65 /*
66 for(int n=0;n<50;n++){
67 printf("\7");
68 Sleep(1);
69 } */
70 #endif
71 rplog -> Print( 1 , " %s Occur,Be Push Into Queue To Be Processed! " ,
72 pdu -> specific_type == EVENT_NODE_DOWN_ID ? " EVENT_NODE_DOWN_ID " : " EVENT_IF_DOWN_ID " );
73 _evctx_list.push_back(ec);
74 DumpDownEvent2File();
75 _evctx_lock.Unlock();
76 } else {
77 delete ec;
78 }
79 }
80
81 // --记录唯一的数据库编号,转存到本地文件
82 void GrabEvent::DumpDownEvent2File(){
83 FILE * fp;
84 EventContext_List::iterator itr;
85 EventContext * ec;
86
87 fp = fopen(FILE_RELOAD, " w " );
88 if ( ! fp ) return ;
89 rplog -> Print( 1 , " DumpList size:%d " ,_evctx_list.size());
90 for ( itr = _evctx_list.begin() ;itr != _evctx_list.end();itr ++ ){
91 ec = * itr;
92 // rplog->Print(1,"Dump Record ID:%s",ec->rid.c_str());
93 fwrite(ec -> rid.c_str(),ec -> rid.size(), 1 ,fp);
94 fwrite( " \n " ,strlen( " \n " ), 1 ,fp);
95 }
96 fclose(fp);
97 }
98
99 // --记录唯一的数据库编号,转存到本地文件
100 void GrabEvent::DumpFile2DownEvent(){
101 FILE * fp;
102 RPSTRING str;
103 // EventContext * ec;
104 char line[ 1024 ];
105 char * chars;
106
107 fp = fopen(FILE_RELOAD, " r " );
108 if ( ! fp ) return ;
109
110 while ( chars = fgets(line,sizeof(line),fp) ){
111 str = ValidString(chars);
112 LoadDBRecord(str);
113 // ec = new EventContext;
114 // ec->rid = str;
115 // _evctx_list.push_back(ec);
116 }
117 fclose(fp);
118 }
119
120
121 void GrabEvent::Run( void * ){
122 struct fd_set read_fds;
123 int count, num_snmp_fds;
124 struct timeval timeout;
125 rplog -> Print( 1 , " GrabEvent Thread Enter! " );
126 while (THREAD_CONTINUE_T) {
127 FD_ZERO( & read_fds);
128 num_snmp_fds = OVsnmpGetRetryInfo( & read_fds, & timeout );
129 if ( num_snmp_fds == 0 ) {
130 SLEEP_MSEC( 200 );
131 // fprintf(stderr, "testapp: No SNMP Sessions open. Goodbye.\n");
132 continue ;
133 }
134 // rplog->Print(1,"Block to Select");
135 count = select(num_snmp_fds, & read_fds, NULL, NULL, NULL);
136 // rplog->Print(1,"Something Arrived,Select it");
137 if (count < 0 ) {
138 perror( " select " );
139 // OVsnmpClose(session);
140 break ;
141 } else {
142 OVsnmpRead( & read_fds);
143 }
144 }
145 rplog -> Print( 1 , " GrabEvent Thread will exit " );
146 _update_thr -> Terminate();
147 _update_thr -> Wait();
148 rplog -> Print( 1 , " GrabEvent has been exited! " );
149 }
150
151 int GrabEvent::Connect(SysConf * conf){
152 const char * filter = " {ALL} .* " ;
153 _conf = conf;
154 // -- parse trapd.conf
155 _ep_list.insert(EventDetail_List::value_type(EVENT_NODE_DOWN_ID, & _tp_node_down));
156 _ep_list.insert(EventDetail_List::value_type(EVENT_NODE_UP_ID, & _tp_node_up));
157 _ep_list.insert(EventDetail_List::value_type(EVENT_IF_DOWN_ID, & _tp_if_down));
158 _ep_list.insert(EventDetail_List::value_type(EVENT_IF_UP_ID, & _tp_if_up));
159 LoadEventDetail(EVENT_NODE_DOWN_ID, & _tp_node_down);
160 LoadEventDetail(EVENT_NODE_UP_ID, & _tp_node_up);
161 LoadEventDetail(EVENT_IF_DOWN_ID, & _tp_if_down);
162 LoadEventDetail(EVENT_IF_UP_ID, & _tp_if_up);
163 // --加载目前未up的信息
164 // MessageBox(0,"begin to dumpfile2downevent",0,MB_OK);
165 DumpFile2DownEvent();
166 // MessageBox(0,"after dumpfile2downevent",0,MB_OK);
167
168
169 // --
170 session = OVsnmpEventOpen(conf -> nnm_host.c_str(),
171 " report-patch " ,
172 GrabThread::pdu_process_entry,
173 ( void * ) this ,
174 filter );
175 if (session == NULL) {
176 return false ;
177 }
178 _update_thr = new UpdateThread( this );
179 _update_thr -> Create();
180 NVThread::Create();
181 return true ;
182 }
183
184 // -- add on 05.08.10 13:00
185 void GrabEvent::LoadDBRecord(RPSTRING & rid){
186 // first start up
187 // TableEventDownUp * downup;
188 SaRetT rc;
189 SaCursorT * scur;
190 char * EVENT_UUID;
191 int EVENT_TIMESTAMP;
192 int CATEGORY;
193 char * NODENAME;
194 int APPLICATION_ID;
195 char * MESSAGE;
196 int SEVERITY;
197 char * EVENT_OID;
198 char * OV_OBJECTID;
199 char * PROTOCOL;
200 char * EVENT_TYPE;
201 char * IP_ADDRESS;
202 int TRAPSOURCE;
203 char * TRAP_NAME;
204 int PID;
205 char * FORWARD_ADDRESS;
206 char * EVENT_SOURCE;
207 char * EVENT_TIME;
208 int NUMBER_VARBINDS;
209 int IFNUMBER;
210
211 char * fs[ 18 ];
212 // char buff[128];
213 memset(fs, 0 ,sizeof( char * ) * 18 );
214 // --load downEvent from dump-file
215 EventContext * ec;
216 // EventContext_List::iterator itr;
217 // for( itr = _evctx_list.begin() ;itr!= _evctx_list.end();itr++){
218 // ec = *itr;
219
220 EVENT_UUID = ( char * )rid.c_str();
221
222 scur = SaCursorCreate(dbscon, " NNM_EVENT_DOWNUP " );
223 rc = SaCursorColStr(scur, " EVENT_UUID " , & EVENT_UUID);
224 rc = SaCursorColInt(scur, " EVENT_TIMESTAMP " , & EVENT_TIMESTAMP);
225 rc = SaCursorColInt(scur, " CATEGORY " , & CATEGORY);
226 rc = SaCursorColStr(scur, " NODENAME " , & NODENAME);
227 rc = SaCursorColInt(scur, " APPLICATION_ID " , & APPLICATION_ID);
228 rc = SaCursorColStr(scur, " MESSAGE " , & MESSAGE);
229 rc = SaCursorColInt(scur, " SEVERITY " , & SEVERITY);
230 rc = SaCursorColStr(scur, " EVENT_OID " , & EVENT_OID);
231 rc = SaCursorColStr(scur, " OV_OBJECTID " , & OV_OBJECTID);
232 rc = SaCursorColStr(scur, " PROTOCOL " , & PROTOCOL);
233 rc = SaCursorColStr(scur, " EVENT_TYPE " , & EVENT_TYPE);
234 rc = SaCursorColStr(scur, " IP_ADDRESS " , & IP_ADDRESS);
235 rc = SaCursorColInt(scur, " TRAPSOURCE " , & TRAPSOURCE);
236 rc = SaCursorColStr(scur, " TRAP_NAME " , & TRAP_NAME);
237 rc = SaCursorColInt(scur, " PID " , & PID);
238 rc = SaCursorColStr(scur, " FORWARD_ADDRESS " , & FORWARD_ADDRESS);
239 rc = SaCursorColStr(scur, " EVENT_SOURCE " , & EVENT_SOURCE);
240 rc = SaCursorColStr(scur, " EVENT_TIME " , & EVENT_TIME);
241 rc = SaCursorColInt(scur, " NUMBER_VARBINDS " , & NUMBER_VARBINDS);
242 rc = SaCursorColInt(scur, " IFNUMBER " , & IFNUMBER);
243 // -- bind variables from F1..F18
244 /*
245 for( int n =0; n< 18; n++){
246 sprintf(buff,"F%d",n+1);
247 rc = SaCursorColStr(scur,buff,&fs[n]);
248 } */
249
250
251 rc = SaCursorOpen(scur);
252 rc = SaCursorEqual(scur, " EVENT_UUID " );
253 rc = SaCursorSearch(scur);
254 if ( SaCursorNext(scur) != SA_RC_SUCC){
255 SaCursorFree(scur);
256 // continue;
257 return ;
258 }
259
260 {
261 ec = new EventContext;
262 ec -> rid = rid;
263 ec -> reload = new TableEventDownUp;
264 ec -> reload -> TRAPSOURCE = TRAPSOURCE ;
265 ec -> reload -> APPLICATION_ID = APPLICATION_ID ;
266 ec -> reload -> PID = PID ;
267 ec -> reload -> CATEGORY = CATEGORY ;
268 ec -> reload -> SEVERITY = SEVERITY ;
269 ec -> reload -> EVENT_TIMESTAMP = EVENT_TIMESTAMP;
270 ec -> reload -> NUMBER_VARBINDS = NUMBER_VARBINDS;
271 ec -> reload -> IFNUMBER = IFNUMBER ;
272 ec -> reload -> TRAP_NAME = TRAP_NAME ;
273 ec -> reload -> FORWARD_ADDRESS = FORWARD_ADDRESS;
274 ec -> reload -> EVENT_SOURCE = EVENT_SOURCE ;
275 ec -> reload -> EVENT_TIME = EVENT_TIME ;
276 ec -> reload -> EVENT_UUID = EVENT_UUID ;
277 ec -> reload -> NODENAME = NODENAME ;
278 ec -> reload -> MESSAGE = MESSAGE ;
279 ec -> reload -> EVENT_OID = EVENT_OID ;
280 ec -> reload -> OV_OBJECTID = OV_OBJECTID ;
281 ec -> reload -> PROTOCOL = PROTOCOL ;
282 ec -> reload -> EVENT_TYPE = EVENT_TYPE ;
283 ec -> reload -> IP_ADDRESS = IP_ADDRESS ;
284 // -=- load fields from F1..F18,data copy deeply
285 /*
286 for(int n=0;n<18;n++){
287 strcpy(ec->reload->fs[n],fs[n]);
288 } */
289 // -- have read out field's values
290 }
291 SaCursorFree(scur);
292 if ( strcmp(EVENT_OID, " 1.3.6.1.4.1.11.2.17.1.0.58916865 " ) == 0 ){
293 ec -> event = EVENT_NODE_DOWN_ID;
294 } else if ( strcmp(EVENT_OID, " 1.3.6.1.4.1.11.2.17.1.0.58916867 " ) == 0 ){
295 ec -> event = EVENT_IF_DOWN_ID;
296 } else {
297 delete ec;
298 return ;
299 }
300
301 ec -> pdu = NULL;
302 ec -> check_time = _conf -> up_status_check_interval + time( 0 );
303 _evctx_lock.Lock();
304 _evctx_list.push_back(ec);
305 _evctx_lock.Unlock();
306 // }
307 }
308
309 GrabEvent::GrabEvent(){
310 _starttime = time( 0 );
311 _evuuid = 0 ;
312 }
313
314
315
316 RPSTRING GrabEvent::DoUpdate(EventContext * ec){
317 SaRetT rc ;
318 SaCursorT * scur = NULL;
319 RPSTRING ret;
320 char * EVENT_UUID;
321 int EVENT_TIMESTAMP;
322 int CATEGORY;
323 char * NODENAME;
324 int APPLICATION_ID;
325 char * MESSAGE;
326 int SEVERITY;
327 char * EVENT_OID;
328 char * OV_OBJECTID;
329 char * PROTOCOL;
330 char * EVENT_TYPE;
331 char * IP_ADDRESS;
332 int TRAPSOURCE;
333 char * TRAP_NAME;
334 int PID;
335 char * FORWARD_ADDRESS;
336 char * EVENT_SOURCE;
337 char * EVENT_TIME;
338 int NUMBER_VARBINDS;
339 int IFNUMBER;
340
341 scur = SaCursorCreate(dbscon, " NNM_EVENT_DOWNUP " );
342
343 rc = SaCursorColStr(scur, " EVENT_UUID " , & EVENT_UUID);
344 rc = SaCursorColInt(scur, " EVENT_TIMESTAMP " , & EVENT_TIMESTAMP);
345 rc = SaCursorColInt(scur, " CATEGORY " , & CATEGORY);
346 rc = SaCursorColStr(scur, " NODENAME " , & NODENAME);
347 rc = SaCursorColInt(scur, " APPLICATION_ID " , & APPLICATION_ID);
348 rc = SaCursorColStr(scur, " MESSAGE " , & MESSAGE);
349 rc = SaCursorColInt(scur, " SEVERITY " , & SEVERITY);
350 rc = SaCursorColStr(scur, " EVENT_OID " , & EVENT_OID);
351 rc = SaCursorColStr(scur, " OV_OBJECTID " , & OV_OBJECTID);
352 rc = SaCursorColStr(scur, " PROTOCOL " , & PROTOCOL);
353 rc = SaCursorColStr(scur, " EVENT_TYPE " , & EVENT_TYPE);
354 rc = SaCursorColStr(scur, " IP_ADDRESS " , & IP_ADDRESS);
355 rc = SaCursorColInt(scur, " TRAPSOURCE " , & TRAPSOURCE);
356 rc = SaCursorColStr(scur, " TRAP_NAME " , & TRAP_NAME);
357 rc = SaCursorColInt(scur, " PID " , & PID);
358 rc = SaCursorColStr(scur, " FORWARD_ADDRESS " , & FORWARD_ADDRESS);
359 rc = SaCursorColStr(scur, " EVENT_SOURCE " , & EVENT_SOURCE);
360 rc = SaCursorColStr(scur, " EVENT_TIME " , & EVENT_TIME);
361 rc = SaCursorColInt(scur, " NUMBER_VARBINDS " , & NUMBER_VARBINDS);
362 rc = SaCursorColInt(scur, " IFNUMBER " , & IFNUMBER);
363
364
365 rc = SaCursorOpen(scur);
366 if ( rc != SA_RC_SUCC){
367 rplog -> Print( 1 , " ReloadSpeedContext::SaCursorOpen Failed! " );
368 return 0 ;
369 }
370 EVENT_TIMESTAMP = GetEventTimestamp(ec);
371 CATEGORY = GetCategory(ec);
372 APPLICATION_ID = GetAppID(ec);
373 SEVERITY = GetSeverity(ec);
374 TRAPSOURCE = GetTrapSource(ec);
375 PID = GetPID(ec);
376 NUMBER_VARBINDS = GetNumVars(ec);
377 IFNUMBER = GetIfNumber(ec);
378 EVENT_UUID = new char [FIELD_STRING_SIZE];
379 strcpy(EVENT_UUID,GetEventUUID().c_str());
380 NODENAME = new char [FIELD_STRING_SIZE];
381 strcpy(NODENAME ,GetNodeName(ec).c_str());
382 MESSAGE = new char [FIELD_STRING_SIZE];
383 strcpy(MESSAGE,GetMessage(ec).c_str());
384 EVENT_OID = new char [FIELD_STRING_SIZE];
385 strcpy(EVENT_OID,GetEvent_OID(ec).c_str());
386 OV_OBJECTID = new char [FIELD_STRING_SIZE];
387 strcpy(OV_OBJECTID,GetOVObjectID(ec).c_str());
388 PROTOCOL = new char [FIELD_STRING_SIZE];
389 strcpy(PROTOCOL,GetProtocal(ec).c_str());
390 EVENT_TYPE = new char [FIELD_STRING_SIZE];
391 strcpy(EVENT_TYPE,GetEventType(ec).c_str());
392 IP_ADDRESS = new char [FIELD_STRING_SIZE];
393 strcpy(IP_ADDRESS,GetIpAddress(ec).c_str());
394 TRAP_NAME = new char [FIELD_STRING_SIZE];
395 strcpy(TRAP_NAME,GetTrapName(ec).c_str());
396 FORWARD_ADDRESS = new char [FIELD_STRING_SIZE];
397 strcpy(FORWARD_ADDRESS,GetForwardAddress(ec).c_str());
398 EVENT_SOURCE = new char [FIELD_STRING_SIZE];
399 strcpy(EVENT_SOURCE,GetEventSource(ec).c_str());
400 EVENT_TIME = new char [FIELD_STRING_SIZE];
401 strcpy(EVENT_TIME,GetEventTimeString(ec).c_str());
402
403 rc = SaCursorInsert(scur);
404 rc = SaTransCommit(dbscon);
405 SaCursorFree(scur);
406
407 ret = EVENT_UUID;
408
409 delete[] EVENT_UUID;
410 delete[] NODENAME;
411 delete[] MESSAGE;
412 delete[] EVENT_OID;
413 delete[] OV_OBJECTID;
414 delete[] PROTOCOL;
415 delete[] EVENT_TYPE;
416 delete[] IP_ADDRESS;
417 delete[] TRAP_NAME;
418 delete[] FORWARD_ADDRESS;
419 delete[] EVENT_SOURCE;
420 delete[] EVENT_TIME;
421 /*
422 //--add on 2005.08.10
423 if( ec->reload == NULL){
424 //--写入pdu所有参数进表 F1..F18
425 PduVars2DB(ec,ret.c_str());
426 } */
427
428 return ret;
429 }
430
431 int GrabEvent::PduVars2DB(EventContext * ec, const char * rid){
432 char * fs[ 18 ];
433 char buff[ 128 ];
434 int n;
435
436 OVsnmpVarBind * var;
437 memset(fs, 0 ,sizeof( char * ) * 18 );
438
439
440 SaRetT rc;
441 SaCursorT * scur;
442
443 char * pid;
444 pid = ( char * )rid;
445 scur = SaCursorCreate(dbscon, " NNM_EVENT_DOWNUP " );
446 rc = SaCursorColStr(scur, " EVENT_UUID " , & pid);
447
448 for ( n = 0 ;n < 18 ;n ++ ){
449 sprintf(buff, " F%d " ,n + 1 );
450 rc = SaCursorColStr(scur,buff, & fs[n]);
451 }
452 rc = SaCursorOpen(scur);
453 rc = SaCursorEqual(scur, " EVENT_UUID " );
454 rc = SaCursorSearch(scur);
455 if ( SaCursorNext(scur) != SA_RC_SUCC){
456 SaCursorFree(scur);
457 return false ;
458 }
459 for ( n = 0 ;n < 18 ;n ++ ){
460 fs[n] = new char [FIELD_STRING_SIZE];
461 memset(fs[n], 0 ,FIELD_STRING_SIZE);
462 }
463 n = 0 ;
464 var = ec -> pdu -> variables;
465 while ( var ){
466 sprint_by_type(fs[n],FIELD_STRING_SIZE,var,VAL_ONLY);
467 n ++ ;
468 var = var -> next_variable;
469 }
470 rc = SaCursorUpdate(scur);
471
472 for ( n = 0 ;n < 18 ;n ++ ){
473 delete[] fs[n];
474 }
475 return true ;
476 }
477
478 // --明天做
479 void GrabEvent::GetCategorySeverity(EventDetail * ed, char * line){
480 if ( strstr(line, " LOGONLY " )){
481 ed -> cat.id = 1 ;
482 } else if ( strstr(line, " Error Alarms " )){
483 ed -> cat.id = 2 ;
484 } else if ( strstr(line, " Threshold Alarms " )){
485 ed -> cat.id = 3 ;
486 } else if ( strstr(line, " Status Alarms " )){
487 ed -> cat.id = 4 ;
488 } else if ( strstr(line, " Configuration Alarms " )){
489 ed -> cat.id = 5 ;
490 } else if ( strstr(line, " Application Alert Alarms " )){
491 ed -> cat.id = 6 ;
492 }
493 // ----
494 if ( strstr(line, " Normal " ) ){
495 ed -> seve.id = 1 ;
496 } else if ( strstr(line, " Warning " ) ){
497 ed -> seve.id = 2 ;
498 } else if ( strstr(line, " Minor " ) ){
499 ed -> seve.id = 3 ;
500 } else if ( strstr(line, " Major " ) ){
501 ed -> seve.id = 4 ;
502 } else if ( strstr(line, " Critical " ) ){
503 ed -> seve.id = 5 ;
504 }
505 }
506
507 void GrabEvent::LoadEventDetail( int ev,EventDetail * ed){
508 FILE * fp;
509 char line[ 1024 ];
510 char buff[ 1024 ];
511 char * occur;
512 char * s; // ,*e;
513 char * p = 0 ;
514 if ( _conf -> trapd == "" ){
515 printf( " \n error: read trapd.conf failed! " );
516 exit( 2 );
517 }
518 fp = fopen(_conf -> trapd.c_str(), " r " );
519 if ( ! fp ){
520 printf( " \n error: read trapd.conf fopen failed! %s " ,_conf -> trapd.c_str());
521 exit( 2 );
522 }
523 while ( occur = fgets(line,sizeof(line),fp)){
524 sprintf(buff, " %u " ,ev);
525 if ( p = strstr(line,buff) ){
526 // -- deal with first line,grab category and severity
527 GetCategorySeverity(ed,line);
528 // -- get next line
529 occur = fgets(line,sizeof(line),fp);
530 if ( ! occur){
531 break ;
532 }
533 // -- to parse parameters
534
535 EventFormatSplitedPart * esp;
536 if ( strstr(occur, " FORMAT " ) ){
537 occur += strlen( " FORMAT " ) + 1 ;
538 }
539 for (s = occur; * s != ' \0 ' ;s ++ ) {
540 if ( * s == ' $ ' ){
541 p = s;
542 continue ;
543 }
544 if ( p && ! isdigit( * s) && (s - p) > 1 ){
545 {
546 if ( p - occur > 0 ){
547 esp = new EventFormatSplitedPart;
548 esp -> type = EventFormatSplitedPart::PROMPT;
549 memset(buff, 0 ,p - occur + 1 );
550 memcpy(buff,occur,p - occur);
551 esp -> val = buff;
552 ed -> list.push_back(esp);
553 }
554 }
555 {
556 memset(buff, 0 ,s - p);
557 memcpy(buff,p + 1 ,s - p - 1 );
558 esp = new EventFormatSplitedPart;
559 esp -> type = EventFormatSplitedPart::PARAMETER;
560 esp -> val = buff;
561 ed -> list.push_back(esp);
562
563 p = 0 ;
564 if ( * s == ' $ ' ){
565 s -- ;
566 }
567 }
568 occur = s;
569 } // -- end if
570 } // -- end for
571 if ( (strlen(line) + line) > occur){
572 esp = new EventFormatSplitedPart;
573 esp -> type = EventFormatSplitedPart::PROMPT;
574 esp -> val = occur;
575 ed -> list.push_back(esp);
576 }
577 }
578 }
579 fclose(fp);
580 }
581
582 RPSTRING GrabEvent::GetEventUUID(){
583 char buff[ 512 ];
584 sprintf(buff, " %x-%x-000d-71da-0f4e-c0a80e890000 " ,_starttime, ++ _evuuid);
585 return RPSTRING(buff);
586 }
587
588 int GrabEvent::GetEventTimestamp(EventContext * ec){
589 int ret;
590 // OVsnmpVarBind* var;
591 // char buff[20];
592 ret = 0 ;
593 /*
594 memset(buff,0,sizeof(buff));
595 var = GetVarBindByIndex(5,ec->pdu);
596 if( var->type == ASN_OCTET_STR){
597 sprintf(buff,(char*)var->val.string,var->val_len);
598 ret = atoi(buff);
599 } */
600 ret = time( 0 );
601 return ret;
602 }
603
604 int GrabEvent::GetCategory(EventContext * ec){
605 int ret;
606 ret = 0 ;
607 EventDetail * ed = _ep_list[ec -> event];
608 if ( ed ) ret = ed -> cat.id;
609 return ret;
610 }
611 int GrabEvent::GetAppID(EventContext * ec){
612 int ret = 0 ;
613 OVsnmpVarBind * var;
614 if ( ec -> reload ){
615 return ec -> reload -> APPLICATION_ID;
616 }
617 var = GetVarBindByIndex( 1 ,ec -> pdu);
618 if ( var )
619 ret = * var -> val.integer;
620 return ret;
621 }
622
623 int GrabEvent::GetSeverity(EventContext * ec){
624 int ret = 0 ;
625 EventDetail * ed = _ep_list[ec -> event];
626 if ( ed ) ret = ed -> seve.id;
627 return ret;
628 }
629 int GrabEvent::GetTrapSource(EventContext * ec){
630 int ret = 0 ;
631 return ret;
632 }
633 int GrabEvent::GetPID(EventContext * ec){
634 int ret = 0 ;
635 return ret;
636 }
637
638 int GrabEvent::GetNumVars(EventContext * ec){
639 int ret = 0 ;
640 OVsnmpVarBind * var;
641 int n;
642 n = 0 ;
643 if ( ec -> reload){
644 return ec -> reload -> NUMBER_VARBINDS;
645 }
646 var = ec -> pdu -> variables;
647 while ( var ){
648 n ++ ;
649 var = var -> next_variable;
650 }
651 ret = n;
652 return ret;
653 }
654
655 int GrabEvent::GetIfNumber(EventContext * ec){
656 OVwFieldId fid ;
657 OVwObjectId oid ;
658 RPSTRING ip;
659 OVsnmpVarBind * var;
660 OVwFieldValue * fv;
661 char buff[ 128 ];
662
663 int ret = 0 ;
664
665 if ( ec -> reload){
666 return ec -> reload -> IFNUMBER;
667 }
668
669 if ( ec -> event == EVENT_NODE_DOWN_ID || ec -> event == EVENT_NODE_UP_ID){
670 ret = 0 ;
671 } else if ( ec -> event == EVENT_IF_DOWN_ID || ec -> event == EVENT_IF_UP_ID){
672 var = GetVarBindByIndex( 6 ,ec -> pdu);
673 if ( ! var ){
674 return false ;
675 }
676 if ( var -> type == ASN_OCTET_STR){
677 buff[var -> val_len] = ' \0 ' ;
678 memcpy(buff,var -> val.string,var -> val_len);
679 oid = atoi(buff);
680 } else if (var -> type == ASN_INTEGER){
681 oid = * var -> val.integer;
682 } else {
683 return ret;
684 }
685 fid = OVwDbFieldNameToFieldId( " SNMP ifIndex " );
686 fv = OVwDbGetFieldValue(oid,fid);
687 if ( fv) {
688 ret = fv -> un.int_val;
689 }
690 }
691 return ret;
692 }
693
694 RPSTRING GrabEvent::GetNodeName(EventContext * ec){
695 RPSTRING ret = " Unknow NodeName " ;
696 char buf[ 2048 ];
697 int buflen = sizeof(buf);
698 OVsnmpVarBind * var;
699
700 if ( ec -> reload){
701 return ec -> reload -> NODENAME;
702 }
703 var = GetVarBindByIndex( 2 ,ec -> pdu);
704 if ( var )
705 sprint_by_type(buf, buflen, var, TYPE_VAL);
706 ret = buf;
707 return ret;
708 }
709
710 RPSTRING GrabEvent::GetMessage(EventContext * ec){
711 RPSTRING ret = "" ;
712 char buf[ 2048 ];
713 OVsnmpVarBind * var;
714 int idx;
715 int n;
716 int buflen = sizeof(buf);
717 EventDetail * ed = _ep_list[ec -> event];
718 if ( ec -> reload ){
719 return ec -> reload -> MESSAGE;
720 }
721
722 for (n = 0 ;n < ed -> list.size();n ++ ){
723 if ( ed -> list.at(n) -> type == EventFormatSplitedPart::PROMPT){
724 ret << ed -> list.at(n) -> val << " " ;
725 continue ;
726 }
727 idx = atoi(ed -> list.at(n) -> val.c_str());
728 var = GetVarBindByIndex(idx,ec -> pdu);
729 if ( var ){
730 sprint_by_type(buf,buflen,var,VAL_ONLY);
731 ret << RPSTRING( " " ) << buf;
732 }
733 }
734 return ret;
735 }
736
737 RPSTRING GrabEvent::GetEvent_OID(EventContext * ec){
738 RPSTRING ret;
739
740 switch (ec -> event){
741 case EVENT_NODE_DOWN_ID:
742 ret = " 1.3.6.1.4.1.11.2.17.1.0.58916865 " ;
743 break ;
744 case EVENT_NODE_UP_ID:
745 ret = " 1.3.6.1.4.1.11.2.17.1.0.58916864 " ;
746 break ;
747 case EVENT_IF_DOWN_ID:
748 ret = " 1.3.6.1.4.1.11.2.17.1.0.58916867 " ;
749 break ;
750
751 case EVENT_IF_UP_ID:
752 ret = " 1.3.6.1.4.1.11.2.17.1.0.58916866 " ;
753 }
754 return ret;
755 }
756
757 RPSTRING GrabEvent::GetOVObjectID(EventContext * ec){
758 RPSTRING ret = " 0 " ;
759 char buff[ 1024 ];
760 OVsnmpVarBind * var;
761 if ( ec -> reload){
762 return ec -> reload -> OV_OBJECTID;
763 }
764
765 if ( ec -> event == EVENT_NODE_DOWN_ID || ec -> event == EVENT_NODE_UP_ID){
766 var = GetVarBindByIndex( 3 ,ec -> pdu);
767 } else if ( ec -> event == EVENT_IF_DOWN_ID || ec -> event == EVENT_IF_UP_ID){
768 var = GetVarBindByIndex( 6 ,ec -> pdu);
769 }
770 if ( ! var ){
771 return ret;
772 }
773
774 if ( var -> type == ASN_OCTET_STR){
775 buff[var -> val_len] = ' \0 ' ;
776 memcpy(buff,var -> val.string,var -> val_len);
777 ret = buff;
778 }
779 return ret;
780 }
781
782 RPSTRING GrabEvent::GetProtocal(EventContext * ec){
783 RPSTRING ret;
784 ret = " Canonical(generic)-event " ;
785 return ret;
786 }
787
788 RPSTRING GrabEvent::GetEventType(EventContext * ec){
789 RPSTRING ret;
790 ret << " . " << GetEvent_OID(ec);
791 return ret;
792 }
793
794 RPSTRING GrabEvent::GetIpAddress(EventContext * ec){
795 RPSTRING ret;
796 in_addr in;
797 if ( ec -> reload){
798 return ec -> reload -> IP_ADDRESS;
799 }
800 in.s_addr = ec -> pdu -> address.sin_addr.s_addr;
801 ret = inet_ntoa(in);
802 return ret;
803 }
804
805 RPSTRING GrabEvent::GetTrapName(EventContext * ec){
806 RPSTRING ret = "" ;
807 switch (ec -> event){
808 case EVENT_NODE_DOWN_ID:
809 ret = " OV_Node_Down " ;
810 break ;
811 case EVENT_NODE_UP_ID:
812 ret = " OV_Node_Up " ;
813 break ;
814 case EVENT_IF_DOWN_ID:
815 ret = " OV_IF_Down " ;
816 break ;
817 case EVENT_IF_UP_ID:
818 ret = " OV_IF_Up " ;
819 break ;
820 }
821 return ret;
822 }
823
824 RPSTRING GrabEvent::GetForwardAddress(EventContext * ec){
825 RPSTRING ret;
826 ret = " 0.0.0.0 " ;
827 return ret;
828 }
829
830 RPSTRING GrabEvent::GetEventSource(EventContext * ec){
831 RPSTRING ret;
832 ret = " IP " ;
833 return ret;
834 }
835
836 RPSTRING GrabEvent::GetEventTimeString(EventContext * ec){
837 RPSTRING ret;
838 unsigned int t = GetEventTimestamp(ec);
839 char buff[ 100 ];
840 tm * ptm;
841 ptm = localtime((time_t * ) & t);
842 sprintf(buff, " %04d-%02d-%02d %02d:%02d:%02d " ,ptm -> tm_year + 1900 ,ptm -> tm_mon + 1 ,ptm -> tm_mday,ptm -> tm_hour,ptm -> tm_min,ptm -> tm_sec);
843 ret = buff;
844 return ret;
845 }
846
847
848 int GrabEvent::IsUp(EventContext * ec){
849 // -- to do check ,monday work,eyes litte tired
850 // -- invoke ovw functions
851
852 OVwFieldId fid ;
853 OVwObjectId oid ;
854 RPSTRING ip;
855 OVsnmpVarBind * var;
856 OVwFieldValue * fv;
857 char buff[ 128 ];
858
859 if ( ec -> reload){ // --历史加载数据
860 oid = atoi(ec -> reload -> OV_OBJECTID.c_str());
861 } else {
862 if ( ec -> event == EVENT_NODE_DOWN_ID){
863 var = GetVarBindByIndex( 3 ,ec -> pdu);
864 } else if ( ec -> event == EVENT_IF_DOWN_ID){
865 var = GetVarBindByIndex( 6 ,ec -> pdu);
866 }
867 if ( ! var ){
868 return false ;
869 }
870 buff[var -> val_len] = ' \0 ' ;
871 memcpy(buff,var -> val.string,var -> val_len);
872 oid = atoi(buff);
873 }
874 fid = OVwDbFieldNameToFieldId( " IP Status " );
875 fv = OVwDbGetFieldValue(oid,fid);
876 if ( fv ){
877 if ( fv -> un.enum_val != 0 && fv -> un.enum_val != 4 ){
878 rplog -> Print( 1 , " [%s]OpenView ObjectID [%d] status is UP! " ,
879 ec -> event == EVENT_NODE_DOWN_ID ? " EVENT_NODE_DOWN_ID " : " EVENT_IF_DOWN_ID " ,
880 oid);
881 return true ;
882 }
883 }
884 /*
885 var = GetVarBindByIndex(2,ec->pdu){
886 if( var->type == ASN_OCTET_STR){
887 buff[var->val_len] ='\0';
888 memcpy(buff,var->val.string,var->val_len);
889 fid = OVwDbFieldNameToFieldId("IP Status");
890 oid = OVwDbHostnameToObjectId(buff);
891 fv = OVwDbGetFieldValue(oid,fid);
892 rplog->Print(1,"Test Host Node[%s] Status ",buff);
893 if( fv ){
894 if( fv->un.enum_val != 0 && fv->un.enum_val!=4){
895 rplog->Print(1,"Node[%s] status is UP!",buff);
896 return true;
897 }
898 }
899 } */
900 return false ;
901 }
902
903 void GrabEvent::Close(){
904 _update_thr -> Terminate();
905 _update_thr -> Wait();
906 rplog -> Print( 1 , " terminated upthread ! " );
907 OVsnmpClose(session);
908 Terminate();
909 Wait();
910 }
911
912 RPSTRING GetDetectObjectName(EventContext * ec){
913 OVsnmpVarBind * var;
914 RPSTRING ret;
915 char buff[ 1024 ];
916 ret = "" ;
917 if ( ec -> reload ){
918 ret << ec -> reload -> NODENAME << " :IF=> " << ec -> reload -> IP_ADDRESS;
919 return ret;
920 }
921 var = GetVarBindByIndex( 2 ,ec -> pdu);
922 if ( ! var){
923 return ret;
924 }
925
926 buff[var -> val_len] = ' \0 ' ;
927 memcpy(buff,var -> val.string,var -> val_len);
928 ret = buff;
929
930 if ( ec -> event == EVENT_NODE_DOWN_ID){
931 ;
932 } else {
933 var = GetVarBindByIndex( 7 ,ec -> pdu);
934 if ( ! var || var -> type != ASN_OCTET_STR){
935 ;
936 } else {
937 buff[var -> val_len] = ' \0 ' ;
938 memcpy(buff,var -> val.string,var -> val_len);
939 ret << " -(IF) " ;
940 ret << buff;
941 }
942 }
943 return ret ;
944 }
945 //////////////////////////////////////////////////////////////////////////
946 // status detect thread
947 void UpdateThread::Run( void * ){
948 EventContext_List::iterator itr;
949 while ( THREAD_CONTINUE_T ){
950 SLEEP_MSEC( 100 );
951 _master -> _evctx_lock.Lock();
952 for ( itr = _master -> _evctx_list.begin() ;itr != _master -> _evctx_list.end();
953 itr ++ ){
954 EventContext * ec;
955 ec = * itr;
956
957 time_t now = time( 0 );
958 if ( now >= ec -> check_time){ // should be checked
959 // -- check status
960 // rplog->Print(1,"准备检测状态,事件源:%s,对象:%s",ec->event==EVENT_NODE_DOWN_ID?
961 // "EVENT_NODE_DOWN_ID":"EVENT_IF_DOWN_ID",GetDetectObjectName(ec).c_str());
962 if ( _master -> IsUp(ec) ){
963 // modify event's status
964 if ( ec -> event == EVENT_NODE_DOWN_ID){
965 ec -> event = EVENT_NODE_UP_ID;
966 }
967 if ( ec -> event == EVENT_IF_DOWN_ID){
968 ec -> event = EVENT_IF_UP_ID;
969 }
970 // time will be updated in GetEventTimeStamp()
971 // rplog->Print(1,"设备[%s]已转化为UP状态,开始更新数据库",GetDetectObjectName(ec).c_str());
972 ec -> shift = true ;
973 _master -> DoUpdate(ec);
974 _master -> _evctx_list.erase(itr);
975 delete ec;
976 _master -> DumpDownEvent2File(); // rewrite event file for next-load
977 rplog -> Print( 1 , " 残留轮训队列对象数:%d " ,_master -> _evctx_list.size());
978 break ; // haha : lazy to jump out,wait next
979 } else {
980 // rplog->Print(1,"设备[%s]没有转化为UP状态,继续等待超时",GetDetectObjectName(ec).c_str());
981 ec -> check_time = now + _master -> _conf -> up_status_check_interval;
982 }
983 }
984 }
985 _master -> _evctx_lock.Unlock();
986 }
987 rplog -> Print( 1 , " UpThread Exiting! " );
988 }
989
990 //////////////////////////////////////////////////////////////////////////
991
992 void GrabThread::pdu_process_entry( int type, OVsnmpSession * session, OVsnmpPdu * pdu, void * data){
993 GrabThread * gt = (GrabThread * ) data;
994 gt -> pdu_process(type, session, pdu);
995 }
996
997
998