AgentXcpp  Revision:0.2
Internals Documentation
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Pages
helpers.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2012 Tanjeff-Nicolai Moos <tanjeff@cccmz.de>
3  *
4  * This file is part of the agentXcpp library.
5  *
6  * AgentXcpp is free software: you can redistribute it and/or modify
7  * it under the terms of the AgentXcpp library license, version 1, which
8  * consists of the GNU General Public License and some additional
9  * permissions.
10  *
11  * AgentXcpp is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See the AgentXcpp library license in the LICENSE file of this package
17  * for more details.
18  */
19 #include <boost/date_time/posix_time/posix_time.hpp>
20 
21 #include "helpers.hpp"
22 
23 using boost::date_time::microsec_clock;
24 using boost::posix_time::ptime;
25 using boost::posix_time::time_duration;
26 
27 using namespace agentxcpp;
28 
29 namespace agentxcpp
30 {
31  /**
32  * \internal
33  *
34  * \brief Variable to measure the uptime of the current process.
35  *
36  * This variable is initialized when the executable starts up
37  * and holds the time at which this happened. Afterwards,
38  * the uptime of the process can be calculated.
39  */
40  static ptime process_start_time(microsec_clock<ptime>::universal_time());
41 }
42 
44  {
45  // Calculate uptime
46  time_duration uptime = microsec_clock<ptime>::universal_time()
47  - process_start_time;
48 
49  // Convert uptime to hundreths of seconds
50  TimeTicksValue sysuptime( uptime.total_milliseconds()/10 );
51 
52  // Return result
53  return sysuptime;
54  }
55 
57  boost::optional<uint32_t> specific_trap)
58  {
59  // We need the OID of the SNMPv1 traps. These are defined here.
60  //
61  // First we define a "helper" OID:
62  static const OidValue snmpTraps_oid(snmpMIBObjects_oid, "5");
63  //
64  // Some traps according to RFC 1907:
65  static const OidValue snmpTraps_coldStart_oid(snmpTraps_oid, "1");
66  static const OidValue snmpTraps_warmStart_oid(snmpTraps_oid, "2");
67  static const OidValue snmpTraps_authenticationFailure_oid(snmpTraps_oid, "5");
68  //
69  // Some traps according to RFC 1573:
70  static const OidValue snmpTraps_linkDown_oid(snmpTraps_oid, "3");
71  static const OidValue snmpTraps_linkUp_oid(snmpTraps_oid, "4");
72 
73  // Finally, egpNeighborLoss. According to RC 1907 it is defined in RFC
74  // 1213, however, the latter doesn't define it. On the other hand,
75  // RFC 2089 defines egpNeighborLoss as 1.3.6.1.6.3.1.1.5.6, which is
76  // snmpTraps.6 and corresponds to the comment in RFC 1907, so we use this
77  // one:
78  static const OidValue snmpTraps_egpNeighborLoss_oid(snmpTraps_oid, "6");
79 
80  // calculate the value of snmpTrapOID.0 according to RFC 1908:
81  OidValue value;
82 
83  switch(generic_trap)
84  {
85  case coldStart:
86  value = snmpTraps_coldStart_oid;
87  break;
88  case warmStart:
89  value = snmpTraps_warmStart_oid;
90  break;
91  case linkDown:
92  value = snmpTraps_linkDown_oid;
93  break;
94  case linkUp:
95  value = snmpTraps_linkUp_oid;
96  break;
98  value = snmpTraps_authenticationFailure_oid;
99  break;
100  case egpNeighborLoss:
101  value = snmpTraps_egpNeighborLoss_oid;
102  break;
103  case enterpriseSpecific:
104  if(!specific_trap)
105  {
106  throw(inval_param());
107  }
108  value = enterprises_oid;
109  value.push_back(0);
110  value.push_back(*specific_trap);
111  break;
112  default:
113  // invalid generic_trap value!
114  throw(inval_param());
115  }
116 
117  // Create and return varbind
118  return value;
119  }