-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntitySensor.hpp
More file actions
123 lines (98 loc) · 2.92 KB
/
Copy pathEntitySensor.hpp
File metadata and controls
123 lines (98 loc) · 2.92 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class EntitySensor {
private:
SNMP *snmp;
int snmpinstance;
int entSensorScale;
int entSensorPrecision;
int entSensorStatus;
int entSensorValue;
int entSensorType;
bool isvalid;
public:
EntitySensor(SNMP *snmp, int instance) : snmp(snmp),snmpinstance(instance),
entSensorScale(0),entSensorPrecision(0),entSensorStatus(0),entSensorValue(0),entSensorType(0) {
std::vector<std::string> oidstrings;
oidstrings.push_back("entSensorScale");
oidstrings.push_back("entSensorPrecision");
oidstrings.push_back("entSensorStatus");
oidstrings.push_back("entSensorValue");
oidstrings.push_back("entSensorType");
for(auto &s : oidstrings) {
s.append(".");
s.append(std::to_string(snmpinstance));
}
SNMPResultList *resultlist=snmp->snmpget(oidstrings);
isvalid=resultlist->valid();
if (!isvalid)
return;
for(auto &r : *resultlist) {
if (!r.valid())
continue;
if (r.sminame("entSensorScale")) { entSensorScale=r.smivalue_int(); continue; }
if (r.sminame("entSensorPrecision")) { entSensorPrecision=r.smivalue_int(); continue; }
if (r.sminame("entSensorStatus")) { entSensorStatus=r.smivalue_int(); continue; }
if (r.sminame("entSensorValue")) { entSensorValue=r.smivalue_int(); continue; }
if (r.sminame("entSensorType")) { entSensorType=r.smivalue_int(); continue; }
}
}
double scalepow() const {
switch(entSensorScale) {
case(6): { return -9; };
case(7): { return -6; };
case(8): { return -3; };
case(9): { return 0; };
}
}
bool valid() const {
return isvalid;
}
double value() const {
return entSensorValue*pow(10,-entSensorPrecision);
}
std::string printable() const {
std::ostringstream msg;
double value=this->value();
msg << value;
switch(entSensorScale) {
case(6): { msg << "n"; break; }
case(7): { msg << "µ"; break; }
case(8): { msg << "m"; break; }
}
/* CISCO-ENTITY-SENSOR-MIB */
switch(entSensorType) {
case(4): { msg << 'V'; break; }
case(5): { msg << 'A'; break; }
case(6): {
msg << 'W';
msg << " " << (double) (log10(value/1)*10) << "dBm";
break;
}
case(8): { msg << "°C"; break; }
case(14): { msg << "dBm"; break; }
}
return msg.str();
}
double dBm() const {
double value=this->value();
/* We have a sensor returning Watt - convert it to dBm */
if (entSensorType == 6) {
return (log10(value/1)*10);
}
/* Sensor is in dBm */
if (entSensorType == 14) {
return value;
}
return 0;
}
friend std::ostream &operator<<(std::ostream &os, const EntitySensor &es) {
os << "entSensorType=" << es.entSensorType << " ";
os << "entSensorStatus=" << es.entSensorStatus << " ";
os << "entSensorValue=" << es.entSensorValue << " ";
os << "entSensorScale=" << es.entSensorScale << " ";
os << "entSensorPrecision=" << es.entSensorPrecision << " ";
double value=pow(10, (-es.entSensorScale))*es.entSensorValue;
//os << std::fixed;
os << "Value: " << es.printable() << " ";
return os;
}
};