forked from jackburton79/ocs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Agent.cpp
99 lines (78 loc) · 1.81 KB
/
Agent.cpp
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
/*
* Agent.cpp
*
* Created on: 11/lug/2013
* Author: Stefano Ceccherini
*/
#include "Agent.h"
#include "Configuration.h"
#include "Inventory.h"
#include "Logger.h"
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <unistd.h>
const char* kVersion = "1.8.0";
std::string Agent::sAgentString;
Agent::Agent()
{
}
Agent::~Agent()
{
}
void
Agent::Run()
{
Configuration* config = Configuration::Get();
std::string deviceID = config->DeviceID();
Inventory inventory;
if (!inventory.Initialize())
throw std::runtime_error("Cannot initialize Inventory");
bool noSoftware = (config->KeyValue(CONF_NO_SOFTWARE) == CONF_VALUE_TRUE);
unsigned long waitSeconds = ::strtoul(
config->KeyValue(CONF_WAIT_TIME).c_str(), NULL, 10);
Logger& logger = Logger::GetDefault();
if (waitSeconds > 0) {
logger.LogFormat(LOG_INFO, "Waiting %lu seconds...", waitSeconds);
::sleep(waitSeconds);
}
if (!inventory.Build(noSoftware))
return;
if (config->KeyValue(CONF_OUTPUT_STDOUT) == CONF_VALUE_TRUE)
inventory.Print();
else if (config->LocalInventory()) {
std::string fullFileName = config->OutputFileName();
if (fullFileName[fullFileName.length() - 1] == '/')
fullFileName.append(deviceID).append(".xml");
inventory.Save(fullFileName.c_str());
} else {
inventory.Send(config->ServerURL().c_str());
}
}
/* static */
std::string
Agent::Version()
{
return kVersion;
}
/* static */
std::string
Agent::LegacyAgentString()
{
return "OCS-NG_unified_unix_agent_v";
}
/* static */
std::string
Agent::AgentString()
{
if (sAgentString.empty()) {
std::string agentString = Configuration::Get()->KeyValue(CONF_AGENT_STRING);
if (!agentString.empty())
sAgentString = agentString;
else {
sAgentString = "jack_lite_inventory_agent_v";
sAgentString.append(Version());
}
}
return sAgentString;
}