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