Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / ntpsnmpd / ntpsnmpd.c
blob068c85d5304d550526e65818fcb7f0dce108a247
1 /* $NetBSD$ */
3 /*****************************************************************************
5 * ntpsnmpd.c
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.
14 * This started as a Google Summer of Code 2008 project,
15 * including the ntpsnmpd sub agent and the libntpq library.
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
23 ****************************************************************************/
25 #include <signal.h>
26 #include <sys/time.h>
28 #ifdef SOLARIS /* needed with at least Solaris 8 */
29 #include <siginfo.h>
30 #endif
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>
37 #undef PACKAGE_BUGREPORT
38 #undef PACKAGE_NAME
39 #undef PACKAGE_STRING
40 #undef PACKAGE_TARNAME
41 #undef PACKAGE_VERSION
43 #include <libntpq.h>
44 #include <ntpsnmpd-opts.h>
46 static int keep_running;
48 RETSIGTYPE
49 stop_server(int a) {
50 keep_running = 0;
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
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];
65 int optct = optionProcess(&ntpsnmpdOptions, argc, argv);
66 argc -= optct;
67 argv += optct;
70 if (!HAVE_OPT(NOFORK))
71 background = 1;
73 if (!HAVE_OPT(SYSLOG))
74 use_syslog = 0;
76 /* using the net-snmp syslog facility */
77 if (use_syslog)
78 snmp_enable_calllog();
79 else
80 snmp_enable_stderrlog();
82 /* Become Subagent */
83 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
85 /* go into background mode, if requested */
86 if (background && netsnmp_daemonize(1, !use_syslog))
87 exit(1);
89 /* Now register with the master Agent X process */
91 /* call Netsnmp socket startup macro, which will initialize the network stuff if required */
92 SOCK_STARTUP;
94 /* Set AgentX socket interface */
95 netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
96 NETSNMP_DS_AGENT_X_SOCKET, "tcp:localhost:705");
98 init_agent("ntpsnmpd");
100 /* Try to connect to ntpd */
101 if ( ntpq_openhost("localhost") == 0 )
103 fprintf(stderr, "Error: Could not connect to ntpd. Aborting.\n");
104 exit(1);
108 /* Register callback functions ... */
109 init_ntpSnmpSubagentObject();
110 init_snmp("ntpsnmpd");
112 /* Signal handler */
113 keep_running = 1;
114 signal(SIGTERM, stop_server);
115 signal(SIGINT, stop_server);
117 snmp_log(LOG_INFO,"ntpsnmpd started.\n");
119 /* main loop here... */
120 while(keep_running) {
121 agent_check_and_process(1); /* 0 == don't block */
124 /* at shutdown time */
125 ntpq_closehost();
126 snmp_shutdown("ntpsnmpd");
127 SOCK_CLEANUP;
129 return 0;