3 /*****************************************************************************
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
20 * Harlan Stenn (Mentor) at stenn@ntp.org
21 * Heiko Gerstung (Student) at gerstung@ntp.org
23 ****************************************************************************/
28 #ifdef SOLARIS /* needed with at least Solaris 8 */
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
40 #undef PACKAGE_TARNAME
41 #undef PACKAGE_VERSION
44 #include <ntpsnmpd-opts.h>
46 static int keep_running
;
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
58 main (int argc
, char **argv
) {
59 int background
= 0; /* start as background process */
60 int use_syslog
= 1; /* use syslog for logging */
65 int optct
= optionProcess(&ntpsnmpdOptions
, argc
, argv
);
70 if (!HAVE_OPT(NOFORK
))
73 if (!HAVE_OPT(SYSLOG
))
76 /* using the net-snmp syslog facility */
78 snmp_enable_calllog();
80 snmp_enable_stderrlog();
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
))
89 /* Now register with the master Agent X process */
91 /* call Netsnmp socket startup macro, which will initialize the network stuff if required */
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");
108 /* Register callback functions ... */
109 init_ntpSnmpSubagentObject();
110 init_snmp("ntpsnmpd");
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 */
126 snmp_shutdown("ntpsnmpd");