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