xref: /netbsd-src/external/bsd/ntp/dist/ntpsnmpd/ntpsnmpd.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: ntpsnmpd.c,v 1.1.1.1 2009/12/13 16:56:33 kardel Exp $	*/
2 
3 /*****************************************************************************
4  *
5  *  ntpsnmpd.c
6  *
7  *  The NTP SNMP daemon is an Agent X subagent application that
8  *  registers itself with a running SNMP Agent X master process running
9  *  on the same machine on port TCP 705. It utilizes the libntqp library
10  *  which accesses status and general data of a running ntpd process on
11  *  the same machine and enables the user to monitor the status of a
12  *  ntp daemon process via SNMP.
13  *
14  *  This started as a Google Summer of Code 2008 project,
15  *  including the ntpsnmpd sub agent and the libntpq library.
16  *
17  *  For more information please visit
18  *	http://support.ntp.org/bin/view/Dev/GSoC2008snmp
19  *  Or contact:
20  *   Harlan Stenn   (Mentor) at stenn@ntp.org
21  *   Heiko Gerstung (Student) at gerstung@ntp.org
22  *
23  ****************************************************************************/
24 
25 #include <signal.h>
26 #include <sys/time.h>
27 
28 #ifdef SOLARIS /* needed with at least Solaris 8 */
29 #include <siginfo.h>
30 #endif
31 
32 #include <net-snmp/net-snmp-config.h>
33 #include <net-snmp/net-snmp-includes.h>
34 #include <net-snmp/agent/net-snmp-agent-includes.h>
35 #include <ntpSnmpSubagentObject.h>
36 
37 #undef PACKAGE_BUGREPORT
38 #undef PACKAGE_NAME
39 #undef PACKAGE_STRING
40 #undef PACKAGE_TARNAME
41 #undef PACKAGE_VERSION
42 
43 #include <libntpq.h>
44 #include <ntpsnmpd-opts.h>
45 
46 static int keep_running;
47 
48 RETSIGTYPE
49 stop_server(int a) {
50     keep_running = 0;
51 }
52 
53 /* The main function just sets up a few things and then enters a loop in which it will
54  * wait for SNMP requests coming from the master agent
55  */
56 
57 int
58 main (int argc, char **argv) {
59   int background = 0; /* start as background process */
60   int use_syslog = 1; /* use syslog for logging */
61   char varvalue[1024];
62 
63 
64 	{
65 		int optct = optionProcess(&ntpsnmpdOptions, argc, argv);
66 		argc -= optct;
67 		argv += optct;
68 	}
69 
70 	if (!HAVE_OPT(NOFORK))
71 		background = 1;
72 
73 	if (!HAVE_OPT(SYSLOG))
74 		use_syslog = 0;
75 
76   /* using the net-snmp syslog facility */
77   if (use_syslog)
78     snmp_enable_calllog();
79   else
80     snmp_enable_stderrlog();
81 
82   /* Become Subagent */
83     netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
84 
85   /* go into background mode, if requested */
86   if (background && netsnmp_daemonize(1, !use_syslog))
87       exit(1);
88 
89   /* Now register with the master Agent X process */
90 
91   /* call Netsnmp socket startup macro, which will initialize the network stuff if required */
92   SOCK_STARTUP;
93 
94   /* Set AgentX socket interface */
95   netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
96                             NETSNMP_DS_AGENT_X_SOCKET, "tcp:localhost:705");
97 
98   init_agent("ntpsnmpd");
99 
100   /* Try to connect to ntpd */
101   if ( ntpq_openhost("localhost") == 0 )
102   {
103 	fprintf(stderr, "Error: Could not connect to ntpd. Aborting.\n");
104 	exit(1);
105   }
106 
107 
108   /* Register callback functions ...  */
109   init_ntpSnmpSubagentObject();
110   init_snmp("ntpsnmpd");
111 
112   /* Signal handler */
113   keep_running = 1;
114   signal(SIGTERM, stop_server);
115   signal(SIGINT, stop_server);
116 
117   snmp_log(LOG_INFO,"ntpsnmpd started.\n");
118 
119   /* main loop here... */
120   while(keep_running) {
121 	agent_check_and_process(1); /* 0 == don't block */
122   }
123 
124   /* at shutdown time */
125   ntpq_closehost();
126   snmp_shutdown("ntpsnmpd");
127   SOCK_CLEANUP;
128 
129   return 0;
130 }
131 
132