summaryrefslogtreecommitdiffstats
path: root/snmp-agent.hh
blob: c75db08616051209d1936c2ab73f893cf96a1c9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once
#include "config.h"

#include <string>
#include <thread>
#include <unistd.h>

#ifdef HAVE_NET_SNMP
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/definitions.h>
#include <net-snmp/types.h>
#include <net-snmp/utilities.h>
#include <net-snmp/config_api.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#undef INET6 /* SRSLY? */
#endif /* HAVE_NET_SNMP */

#include "mplexer.hh"
#include "channel.hh"

class SNMPAgent
{
public:
  SNMPAgent(const std::string& name, const std::string& daemonSocket);
  virtual ~SNMPAgent()
  {
  }

  void run()
  {
#ifdef HAVE_NET_SNMP
  d_thread = std::thread(&SNMPAgent::worker, this);
  d_thread.detach();
#endif /* HAVE_NET_SNMP */
  }

#ifdef HAVE_NET_SNMP
  static int setCounter64Value(netsnmp_request_info* request,
                               uint64_t value);
#endif /* HAVE_NET_SNMP */
protected:
#ifdef HAVE_NET_SNMP
  /* OID for snmpTrapOID.0 */
  static const std::array<oid, 11> snmpTrapOID;

  static bool sendTrap(pdns::channel::Sender<netsnmp_variable_list, void(*)(netsnmp_variable_list*)>& sender,
                       netsnmp_variable_list* varList);

  pdns::channel::Sender<netsnmp_variable_list, void(*)(netsnmp_variable_list*)> d_sender;
  pdns::channel::Receiver<netsnmp_variable_list, void(*)(netsnmp_variable_list*)> d_receiver;
#endif /* HAVE_NET_SNMP */
private:
  void worker();
  static void handleTrapsCB(int fd, FDMultiplexer::funcparam_t& var);
  static void handleSNMPQueryCB(int fd, FDMultiplexer::funcparam_t& var);
  void handleTrapsEvent();
  void handleSNMPQueryEvent(int fd);

  std::thread d_thread;
};