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