1 /*****************************************************************************
5 * The NTP SNMP daemon is an Agent X subagent application that
6 * registers itself with a running SNMP Agent X master process running
7 * on the same machine on port TCP 705. It utilizes the libntqp library
8 * which accesses status and general data of a running ntpd process on
9 * the same machine and enables the user to monitor the status of a
10 * ntp daemon process via SNMP.
12 * This started as a Google Summer of Code 2008 project,
13 * including the ntpsnmpd sub agent and the libntpq library.
15 * For more information please visit
16 * http://support.ntp.org/bin/view/Dev/GSoC2008snmp
18 * Harlan Stenn (Mentor) at stenn@ntp.org
19 * Heiko Gerstung (Student) at gerstung@ntp.org
21 ****************************************************************************/
27 #ifdef SOLARIS /* needed with at least Solaris 8 */
32 #include <ntpsnmpd-opts.h>
34 static int keep_running;
35 RETSIGTYPE stop_server(int);
42 /* The main function just sets up a few things and then enters a loop in which it will
43 * wait for SNMP requests coming from the master agent
47 main (int argc, char **argv) {
48 int background = 0; /* start as background process */
49 int use_syslog = 1; /* use syslog for logging */
52 int optct = optionProcess(&ntpsnmpdOptions, argc, argv);
57 if (!HAVE_OPT(NOFORK))
60 if (!HAVE_OPT(SYSLOG))
63 /* using the net-snmp syslog facility */
65 snmp_enable_calllog();
67 snmp_enable_stderrlog();
70 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
72 /* go into background mode, if requested */
73 if (background && netsnmp_daemonize(1, !use_syslog))
76 /* Now register with the master Agent X process */
78 /* call Netsnmp socket startup macro, which will initialize the network stuff if required */
81 /* Set AgentX socket interface */
82 netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
83 NETSNMP_DS_AGENT_X_SOCKET, OPT_ARG( AGENTXSOCKET ));
85 init_agent("ntpsnmpd");
87 /* Try to connect to ntpd */
88 if ( ntpq_openhost("localhost", 0) == 0 )
90 fprintf(stderr, "Error: Could not connect to ntpd. Aborting.\n");
95 /* Register callback functions ... */
96 init_ntpSnmpSubagentObject();
97 init_snmp("ntpsnmpd");
101 signal(SIGTERM, stop_server);
102 signal(SIGINT, stop_server);
104 snmp_log(LOG_INFO,"ntpsnmpd started.\n");
106 /* main loop here... */
107 while(keep_running) {
108 agent_check_and_process(1); /* 0 == don't block */
111 /* at shutdown time */
113 snmp_shutdown("ntpsnmpd");