xref: /onnv-gate/usr/src/cmd/agents/snmp/trapsend/trapsend.c (revision 1706:0e053bac3f94)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <stdio.h>
36 #include <sys/socket.h>
37 #include <errno.h>
38 #include <syslog.h>
39 #include <string.h>
40 #include <arpa/inet.h>
41 #include <netdb.h>
42 #include <nlist.h>
43 
44 #include "snmp_msg.h"
45 #include "impl.h"
46 #include "trace.h"
47 #include "snmp.h"
48 #include "pdu.h"
49 #include "trap.h"
50 #include "error.h"
51 #include "oid.h"
52 #include "usage.h"
53 
54 
55 #include "sea_i18n.h"
56 
57 
58 static int is_number (char *buf);
59 
60 int
main(int argc,char * argv[])61 main(int argc, char *argv[])
62 {
63     extern char *optarg;
64 	extern int optind;
65 	int opt;
66 
67 	char hostname[MAXHOSTNAMELEN];
68 	IPAddress ip_address;
69 	IPAddress my_ip_addr;
70 	Oid *enterprise;
71 	int generic, specific, level;
72 	SNMP_variable *variables;
73 	struct hostent *hp;
74 	int trap_port = -1;
75 	u_long time_stamp = (u_long)-1;
76 	int enterprise_flag= 0, a_flag = 0, i_flag = 0;
77 
78 	optind = 1;
79 
80 	/* the default host name is local host */
81 	gethostname(hostname, sizeof(hostname));
82 
83 	/* default Oid for enterprise is sun */
84 	enterprise = &sun_oid;
85 
86 	/* generic, specific */
87 	generic = 6;
88 	specific = 1;
89 
90 
91         {
92         char domain_path[MAXPATHLEN];
93 
94         setlocale(LC_ALL, "");
95 
96         sprintf(domain_path, SEA_LOCALE_PATH);
97 
98         bindtextdomain(DOMAIN_MGET, domain_path);
99         bindtextdomain(DOMAIN_SGET,   domain_path);
100         bindtextdomain(DOMAIN_LIBGET,   domain_path);
101         bindtextdomain(DOMAIN_LGET, domain_path);
102         bindtextdomain(DOMAIN_FGET,  domain_path);  /* formatting string */
103         }
104 
105 
106 	/* get command-line options */
107     while ((opt = getopt(argc, argv, "h:c:e:E:g:s:i:t:a:T:p:")) != EOF) {
108 		switch (opt) {
109 			case 'T':
110 				level = atoi(optarg);
111 				if(trace_set(level, error_label)){
112 					fprintf(stderr, " %d is not a valid trace level!\n",
113 							level);
114 					usage();
115 				}
116 				break;
117 			case 'h':		/* host to send trap to */
118 				if (strlcpy(hostname, optarg, sizeof (hostname))
119 					> MAXHOSTNAMELEN) {
120 					fprintf(stderr, "%s: hostname too long!\
121 						\n", optarg);
122 					exit(1);
123 				}
124 			case 'c':
125 				trap_community = optarg;
126 				break;
127 			case 'e':
128 				if (enterprise_flag) {
129 					usage();
130 				}
131 				enterprise = SSAOidStrToOid(optarg,error_label);
132 				if (!enterprise){ /* error */
133 					fprintf(stderr,
134 							"%s: not a valid enterprise oid string!\n",
135 							optarg);
136 					usage();
137 				}
138 				enterprise_flag = 1;
139 				break;
140 			case 'E':
141 				if (enterprise_flag) {
142 					usage();
143 				}
144 				enterprise = get_oid(optarg);
145 				if (!enterprise) {
146 					usage();
147 				}
148 				enterprise_flag = 1;
149 				break;
150 			case 'g':         /* generic trap type */
151 				if (is_number(optarg))
152 					usage();
153 				generic = atoi(optarg);
154 				if ((generic > 6 ) || (generic < 0))
155 					usage();
156 				break;
157 			case 's':         /* specific trap type */
158 				if (is_number(optarg))
159 					usage();
160 				specific = atoi(optarg);
161 				break;
162 			case 'i':
163 				if (name_to_ip_address(optarg,
164 									   &my_ip_addr,
165 									   error_label)) {
166 					usage();
167 				}
168 				i_flag = 1;
169 				break;
170 			case 't': /* timestamp */
171 				time_stamp = atol(optarg);
172 				break;
173 			case 'p':
174 				if (is_number(optarg))
175 					usage();
176 				trap_port = atoi(optarg);
177 				break;
178 			case 'a':         /* attribute information */
179 				if ((variables = get_variable(optarg))== NULL){
180 					fprintf(stderr,
181 							"%s: not a valid variable!\n", optarg);
182 					usage();
183 				}
184 				a_flag = 1;
185 				break;
186 			case '?':		/* usage help */
187 				usage();
188 				break;
189 			default:
190 				usage();
191 				break;
192 		}  /* switch */
193 	}/* while */
194 
195 	if ((optind != argc) || (!a_flag))
196 		usage();
197 
198 	if ((ip_address.s_addr = inet_addr(hostname)) == -1 ) {
199 		if ((hp = gethostbyname(hostname)) == NULL) {
200 			fprintf(stderr, "\n%s is not a valid hostname!\n\n", hostname);
201 			usage();
202 		}
203 		memcpy(&(ip_address.s_addr), hp->h_addr, hp->h_length);
204 	}
205 
206 
207 
208 	/* some trace message */
209 
210 	if (trap_send_with_more_para(&ip_address,
211 								 my_ip_addr, trap_community, i_flag,
212 								 enterprise,
213 								 generic,
214 								 specific,
215 								 trap_port,
216 								 time_stamp,
217 								 variables,
218 								 error_label)) {
219 		fprintf(stderr, "trap_send not success!\n\n");
220 		return (-1);
221 	} else {
222 		return (0);
223 	}
224 
225 /*	if (trap_send(&ip_address,
226 				  enterprise,
227 				  generic,
228 				  specific,
229 				  variables,
230 				  error_label)) {
231 		fprintf(stderr, "trap_send not success!\n\n");
232 	}	 */
233 }
234 
235 
236 
is_number(buf)237 static int is_number (buf)
238 char *buf;
239 {
240 	int len, i;
241 
242 	if (buf == NULL)
243 		return (-1);
244 
245 	len = strlen(buf);
246 	for (i= 0; i < len; i++)
247 		if (!isdigit(buf[i])) {
248 		fprintf(stderr, "\n%s is not a valid generic or specific trap type number!\n\n", buf);
249 		return(-1);
250 		}
251 	return (0);
252 }
253 
254