-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
251 lines (224 loc) · 7.9 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* main.cpp
*
* Created on: 11/lug/2013
* Author: Stefano Ceccherini
*/
#include "Agent.h"
#include "Configuration.h"
#include "Logger.h"
#include <cstdlib>
#include <cstring>
#include <getopt.h>
#include <iostream>
#include <unistd.h>
#include <sys/stat.h>
#include <string>
extern const char* __progname;
static
struct option sLongOptions[] = {
{ "conf", required_argument, 0, 'c' },
{ "server", required_argument, 0, 's' },
{ "local", required_argument, 0, 'l' },
{ "stdout", no_argument, 0, 0 },
{ "format", required_argument, 0, 0 },
{ "tag", required_argument, 0, 't' },
{ "nosoftware", no_argument, 0, 0 },
{ "daemonize", no_argument, 0, 'd' },
{ "wait", required_argument, 0, 'w' },
{ "help", no_argument, 0, 'h' },
{ "logger", required_argument, 0, 0 },
{ "verbose", no_argument, 0, 'v' },
{ "version", no_argument, 0, 0 },
{ "agent-string", required_argument, 0, 0 },
{ "use-current-time-in-device-ID", no_argument, 0, 0 },
{ 0, 0, 0, 0 }
};
static void
PrintHelpAndExit()
{
std::cout << __progname << " " << Agent::Version() << std::endl;
if (geteuid() != 0) {
std::cout << "WARNING: This program should be run as root." << std::endl;
std::cout << std::endl;
}
std::cout << "Usage:" << std::endl;
std::cout << " -h, --help Print usage" << std::endl;
std::cout << " -c, --conf <config_file> Specify configuration file" << std::endl;
std::cout << " -s, --server <server> Specify OCSInventory/GLPI server url" << std::endl;
std::cout << " If the server needs authentication, use the standard syntax <user>:<password>@<host>" << std::endl;
std::cout << " --format <format> Specify the inventory format: FORMAT_OCS or FORMAT_GLPI" << std::endl;
std::cout << " -l, --local <folder> Save a local inventory in the specified file or folder" << std::endl;
std::cout << " --stdout Print inventory to stdout" << std::endl;
std::cout << std::endl;
std::cout << " -t, --tag <TAG> Specify tag. Will be ignored by server if a value already exists" << std::endl;
std::cout << " --nosoftware Do not retrieve installed software" << std::endl;
std::cout << std::endl;
std::cout << " --agent-string <string> Specify custom HTTP agent string" << std::endl;
std::cout << std::endl;
std::cout << " -d, --daemonize Detach from running terminal" << std::endl;
std::cout << " -w, --wait <s> Wait for the specified amount of seconds before building the inventory" << std::endl;
std::cout << std::endl;
std::cout << " --logger <backend> Specify error log backend (STDERR / SYSLOG)." << std::endl;
std::cout << " Default is standard error if attached to a terminal, otherwise syslog. " << std::endl;
std::cout << " -v, --verbose Verbose mode" << std::endl;
std::cout << " --version Print version and exit" << std::endl;
std::cout << std::endl;
std::cout << " --use-current-time-in-device-ID Use current time in the device ID, instead of the BIOS Date." << std::endl;
std::cout << " No need to use this option unless you know why you need it." << std::endl;
std::cout << std::endl;
std::cout << "The -l and -s option are mutually exclusive." << std::endl;
std::cout << "If no server or output file is specified, ";
std::cout << "either via the -s/-l option or via configuration file (option -c), ";
std::cout << "the program will exit without doing anything." << std::endl;
std::cout << std::endl;
std::cout << "Examples:" << std::endl;
std::cout << " Use the configuration file /etc/ocsinventory-ng.conf :" << std::endl;
std::cout << " " << __progname;
std::cout << " --conf /etc/ocsinventory-ng.conf" << std::endl;
std::cout << std::endl;
std::cout << " Send inventory to server http://ocsinventory-ng/ocsinventory :" << std::endl;
std::cout << " " << __progname;
std::cout << " --server http://ocsinventory-ng/ocsinventory" << std::endl;
std::cout << std::endl;
std::cout << " Send inventory to server https://ocsinventory-ng/ocsinventory which requires http basic authentication :" << std::endl;
std::cout << " " << __progname;
std::cout << " --server https://user:password@ocsinventory-ng/ocsinventory" << std::endl;
std::cout << std::endl;
std::cout << " Save a local inventory to /var/tmp/inventoryFile.xml :" << std::endl;
std::cout << " " << __progname;
std::cout << " --local /var/tmp/inventoryFile.xml" << std::endl;
std::cout << std::endl;
std::cout << " Save a local inventory to /var/tmp/<device_id>.xml :" << std::endl;
std::cout << " " << __progname;
std::cout << " --local /var/tmp/" << std::endl;
std::cout << std::endl;
std::cout << " Print inventory to standard output :" << std::endl;
std::cout << " " << __progname;
std::cout << " --stdout" << std::endl;
::exit(0);
}
static void
PrintVersionAndExit()
{
std::cout << __progname << " " << Agent::Version() << std::endl;
::exit(0);
}
static void
HandleArgs(int argc, char **argv)
{
bool verbose = false;
bool daemonize = false;
Configuration* config = Configuration::Get();
int optIndex = 0;
int c = 0;
while ((c = ::getopt_long(argc, argv, "c:s:dDt:l:hvw:",
sLongOptions, &optIndex)) != -1) {
switch (c) {
case 'c':
config->Load(optarg);
break;
case 's':
config->SetServer(optarg);
break;
case 'D':
case 'd':
daemonize = true;
config->SetVolatileKeyValue("DAEMONIZE", "true");
break;
case 't':
config->SetVolatileKeyValue("TAG", optarg);
break;
case 'l':
config->SetOutputFileName(optarg);
break;
case 'h':
PrintHelpAndExit();
break;
case 'v':
verbose = true;
break;
case 'w':
config->SetVolatileKeyValue("waittime", optarg);
break;
case 0:
{
std::string optName = sLongOptions[optIndex].name;
if (optName == "nosoftware")
config->SetVolatileKeyValue("nosoftware", "true");
else if (optName == "format") {
if (::strcasecmp(optarg, "FORMAT_OCS") == 0 ||
::strcasecmp(optarg, "FORMAT_GLPI") == 0) {
config->SetVolatileKeyValue("format", optarg);
}
}
else if (optName == "stdout" && !daemonize)
config->SetVolatileKeyValue("stdout", "true");
else if (optName == "use-current-time-in-device-ID")
config->SetUseCurrentTimeInDeviceID(true);
else if (optName == "agent-string")
config->SetVolatileKeyValue(CONF_AGENT_STRING, optarg);
else if (optName == "logger")
Logger::SetLogger(optarg);
else if (optName == "version")
PrintVersionAndExit();
break;
}
}
}
if (verbose)
Logger::SetLevel(LOG_DEBUG);
else
Logger::SetLevel(LOG_INFO);
bool stdout = config->KeyValue("stdout") == CONF_VALUE_TRUE;
if (!stdout && config->ServerURL().empty()
&& config->LocalInventory() && config->OutputFileName().empty()) {
PrintHelpAndExit();
}
}
static void
Daemonize()
{
pid_t processID = ::fork();
if (processID < 0) {
Logger::Log(LOG_ERR, "Failed to daemonize. Exiting...");
// Return failure in exit status
::exit(1);
}
// Exit the parent process
if (processID > 0)
::exit(0);
::umask(0);
if (::chdir("/") < 0)
; // Ignore
//set new session
pid_t sid = ::setsid();
if (sid < 0)
::exit(1);
::close(STDIN_FILENO);
::close(STDOUT_FILENO);
::close(STDERR_FILENO);
}
int
main(int argc, char **argv)
{
HandleArgs(argc, argv);
if (Configuration::Get()->KeyValue("DAEMONIZE") == CONF_VALUE_TRUE)
Daemonize();
try {
Agent agent;
agent.Run();
#if DEBUG
Configuration::Get()->Print();
std::cout << "Agent String: " << Agent::AgentString() << std::endl;
#endif
} catch (std::exception& ex) {
Logger::Log(LOG_ERR, ex.what());
return 1;
} catch (...) {
Logger::Log(LOG_ERR, "Unhandled exception.");
return 1;
}
Configuration::Get()->Save();
return 0;
}