forked from jackburton79/ocs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachine.cpp
611 lines (474 loc) · 11.2 KB
/
Machine.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
* Machine.cpp
*
* Created on: 11/lug/2013
* Author: stefano
*/
// TODO: Reorganize code.
#include "Machine.h"
#include "ProcReader.h"
#include "Support.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <sstream>
#include <vector>
const char* kBIOSInfo = "BIOS Information";
const char* kSystemInfo = "System Information";
const char* kProcessorInfo = "Processor Info";
const char* kMemoryDevice = "Memory Device";
static Machine* sMachine = NULL;
/* static */
Machine*
Machine::Get()
{
if (sMachine == NULL)
sMachine = new Machine();
return sMachine;
}
Machine::Machine()
:
fNumCPUs(0)
{
_RetrieveData();
}
Machine::~Machine()
{
}
void
Machine::_RetrieveData()
{
if (!_GetDMIDecodeData()) {
std::cerr << "Can't find dmidecode. Is it installed?" << std::endl;
}
_GetLSHWData();
_GetCPUInfo();
try {
_GetOSInfo();
} catch (...) {
std::cerr << "Failed to get kernel info." << std::endl;
}
}
std::string
Machine::AssetTag() const
{
return "";
}
std::string
Machine::BIOSVersion() const
{
return _GetValue("Version", kBIOSInfo);
}
std::string
Machine::BIOSManufacturer() const
{
return _GetValue("Vendor", kBIOSInfo);
}
std::string
Machine::BIOSDate() const
{
return _GetValue("Release Date", kBIOSInfo);
}
std::string
Machine::SystemManufacturer() const
{
return _GetValue("Manufacturer", kSystemInfo);
}
std::string
Machine::HostName() const
{
struct ::utsname utsName;
if (uname(&utsName) != 0)
return "";
return utsName.nodename;
}
std::string
Machine::SystemModel() const
{
return _GetValue("Product Name", kSystemInfo);
}
std::string
Machine::SystemSerialNumber() const
{
return _GetValue("Serial Number", kSystemInfo);
}
std::string
Machine::SystemUUID() const
{
return _GetValue("UUID", kSystemInfo);
}
std::string
Machine::MachineSerialNumber() const
{
return _GetValue("Serial Number", "Base Board Information");
}
std::string
Machine::MachineManufacturer() const
{
return _GetValue("Manufacturer", "Base Board Information");
}
int
Machine::CountProcessors() const
{
return fNumCPUs;
}
std::string
Machine::ProcessorManufacturer(int numCpu) const
{
return _ProcessorInfo("vendor_id", numCpu);
}
std::string
Machine::ProcessorSpeed(int numCpu) const
{
std::string mhz = _ProcessorInfo("cpu MHz", numCpu);
size_t pos = mhz.find(".");
if (pos != std::string::npos) {
mhz = mhz.substr(0, pos);
}
return mhz;
}
std::string
Machine::ProcessorSerialNumber(int numCpu) const
{
//std::string mhz = _GetValue("Serial Number", numCpu);
return "";
}
std::string
Machine::ProcessorType(int numCpu) const
{
std::string model = _ProcessorInfo("model name", numCpu);
trim(model);
return model;
}
os_info
Machine::OSInfo() const
{
return fKernelInfo;
}
int
Machine::CountVideos() const
{
return fVideoInfo.size();
}
video_info
Machine::VideoInfoFor(int numVideo) const
{
return fVideoInfo[numVideo];
}
// private
bool
Machine::_GetDMIDecodeData()
{
if (!CommandExists("dmidecode"))
return false;
try {
popen_streambuf dmi("dmidecode", "r");
std::istream iStream(&dmi);
std::string string;
while (std::getline(iStream, string) > 0) {
// Skip the line with "Handle" in it.
if (string.find("Handle") == std::string::npos) {
std::string header = string;
header = trim(header);
_GetSystemInfo(iStream, header);
}
}
} catch (...) {
return false;
}
return true;
}
bool
Machine::_GetLSHWShortData()
{
if (!CommandExists("lshw"))
return false;
popen_streambuf lshw("lshw -short", "r");
std::istream iStream(&lshw);
try {
std::string line;
// header
std::getline(iStream, line);
size_t devicePos = line.find("Device");
size_t classPos = line.find("Class");
size_t descriptionPos = line.find("Description");
// skip ==== line
std::getline(iStream, line);
while (std::getline(iStream, line) > 0) {
std::string device = line.substr(devicePos, classPos - devicePos);
std::string devClass = line.substr(classPos, descriptionPos - classPos);
std::string value = line.substr(descriptionPos, std::string::npos);
trim(devClass);
if (devClass == "system") {
std::string sysCtx = "Product Name";
sysCtx.append(kSystemInfo);
if (fSystemInfo.find(sysCtx) == fSystemInfo.end()) {
fSystemInfo.insert(std::pair<std::string, std::string>(sysCtx, trim(value)));
}
} else if (devClass == "display") {
struct video_info info;
info.name = trim(value);
info.chipset = "VGA compatible controller";
fVideoInfo.push_back(info);
}
}
} catch (...) {
}
return true;
}
bool
Machine::_GetLSHWData()
{
if (!CommandExists("lshw"))
return false;
popen_streambuf lshw("lshw", "r");
std::istream iStream(&lshw);
try {
std::string line;
std::string context = kSystemInfo;
// skip initial line
std::getline(iStream, line);
while (std::getline(iStream, line) > 0) {
trim(line);
if (size_t start = line.find("*-") != std::string::npos) {
context = line.substr(start + 2, std::string::npos);
if (context == "firmware")
context = kBIOSInfo;
// TODO: Map other contexts to dmidecode ones
// TODO: Make this better.
continue;
}
size_t colonPos = line.find(":");
if (colonPos == std::string::npos)
continue;
// TODO: Better mapping of keys
std::string key = line.substr(0, colonPos);
trim(key);
std::string value = line.substr(colonPos + 1, std::string::npos);
trim(value);
if (key == "vendor") {
std::string sysCtx = "Manufacturer";
sysCtx.append(context);
if (fSystemInfo.find(sysCtx) == fSystemInfo.end()) {
fSystemInfo.insert(std::pair<std::string, std::string>(sysCtx, trim(value)));
}
} else if (key == "product") {
std::string sysCtx = "Product Name";
sysCtx.append(context);
if (fSystemInfo.find(sysCtx) == fSystemInfo.end()) {
fSystemInfo.insert(std::pair<std::string, std::string>(sysCtx, trim(value)));
}
}
}
} catch (...) {
}
return true;
}
void
Machine::_GetCPUInfo()
{
ProcReader cpu("/cpuinfo");
std::istream iStream(&cpu);
std::string string;
int processorNum = 0;
while (std::getline(iStream, string) > 0) {
if (string.find("processor") != std::string::npos) {
fNumCPUs++;
// Get the processor number
size_t pos = string.find(":");
if (pos == std::string::npos)
continue;
std::string valueString = string.substr(pos + 2, std::string::npos);
trim(valueString);
processorNum = ::strtol(valueString.c_str(), NULL, 10);
} else {
size_t pos = string.find(":");
if (pos == std::string::npos)
continue;
try {
std::string name = string.substr(0, pos);
std::string value = string.substr(pos + 1, std::string::npos);
fCPUInfo[processorNum][trim(name)] = trim(value);
} catch (...) {
}
}
}
}
void
Machine::_GetOSInfo()
{
struct utsname uName;
if (::uname(&uName) != 0)
throw errno;
fKernelInfo.hostname = uName.nodename;
fKernelInfo.comments = uName.version;
fKernelInfo.os_release = uName.release;
fKernelInfo.domain_name = uName.domainname;
fKernelInfo.machine = uName.machine;
ProcReader proc("meminfo");
std::istream stream(&proc);
std::string string;
size_t pos;
while (std::getline(stream, string) > 0) {
if ((pos = string.find("SwapTotal:")) != std::string::npos) {
pos = string.find(":");
std::string swapString = string.substr(pos + 1, std::string::npos);
int swapInt = ::strtol(trim(swapString).c_str(), NULL, 10) / 1024;
fKernelInfo.swap = int_to_string(swapInt);
} else if ((pos = string.find("MemTotal:")) != std::string::npos) {
pos = string.find(":");
std::string memString = string.substr(pos + 1, std::string::npos);
int memInt = ::strtol(trim(memString).c_str(), NULL, 10) / 1024;
fKernelInfo.memory = int_to_string(memInt);
}
}
_IdentifyOS();
}
void
Machine::_IdentifyOS()
{
if (CommandExists("lsb_release")) {
popen_streambuf lsb;
lsb.open("lsb_release -a", "r");
std::istream lsbStream(&lsb);
std::string line;
while (std::getline(lsbStream, line) > 0) {
size_t pos = line.find(':');
if (pos != std::string::npos) {
std::string key = line.substr(0, pos);
if (key == "Description") {
std::string value = line.substr(pos + 1, std::string::npos);
fKernelInfo.os_description = trim(value);
}
}
}
} else {
// there is no lsb_release command.
// try to identify the system in another way
if (::access("/etc/thinstation.global", F_OK) != -1)
fKernelInfo.os_description = "Thinstation";
else
fKernelInfo.os_description = "Unknown";
}
}
void
Machine::_GetSystemInfo(std::istream& stream, std::string header)
{
std::string string;
size_t pos = 0;
while (std::getline(stream, string) > 0) {
if (string == "")
break;
pos = string.find(":");
if (pos == std::string::npos)
continue;
try {
std::string name = string.substr(0, pos);
trim(name);
std::string fullString = header;
fullString.append(name);
std::string value = string.substr(pos + 2, std::string::npos);
fSystemInfo.insert(std::pair<std::string, std::string>(trim(fullString), trim(value)));
} catch (...) {
}
}
}
std::string
Machine::_GetValue(std::string string, std::string header) const
{
std::map<std::string, std::string>::const_iterator i;
std::string fullString = header;
fullString.append(string);
i = fSystemInfo.find(fullString);
if (i != fSystemInfo.end())
return i->second;
return "";
}
std::vector<std::string>
Machine::_GetValues(std::string string, std::string header) const
{
std::vector<std::string> stringList;
std::pair <std::multimap<std::string, std::string>::const_iterator,
std::multimap<std::string, std::string>::const_iterator> result;
std::string fullString = header;
fullString.append(string);
result = fSystemInfo.equal_range(fullString);
for (std::multimap<std::string, std::string>::const_iterator i = result.first;
i != result.second; i++) {
stringList.push_back(i->second);
}
return stringList;
}
int
Machine::CountMemories()
{
std::vector<std::string> values = _GetValues("Size", kMemoryDevice);
return values.size();
}
std::string
Machine::MemoryID(int num)
{
return "";
//return _GetValue()
}
std::string
Machine::MemoryCaption(int num)
{
return "";
}
std::string
Machine::MemoryDescription(int num)
{
return "";
}
std::string
Machine::MemoryCapacity(int num)
{
std::vector<std::string> values = _GetValues("Size", kMemoryDevice);
return values.at(num);
}
std::string
Machine::MemoryPurpose(int num)
{
return "";
}
std::string
Machine::MemoryType(int num)
{
std::vector<std::string> values = _GetValues("Type", kMemoryDevice);
return values.at(num);
}
std::string
Machine::MemorySpeed(int num)
{
std::vector<std::string> values = _GetValues("Speed", kMemoryDevice);
return values.at(num);
}
std::string
Machine::MemoryNumSlots(int num)
{
return "";
}
std::string
Machine::MemorySerialNumber(int num)
{
std::vector<std::string> values = _GetValues("Serial Number", kMemoryDevice);
return values.at(num);
}
std::string
Machine::_ProcessorInfo(const char* info, int num) const
{
if (num < 0 || num >= fNumCPUs)
return "";
std::map<std::string, std::string>::const_iterator i;
i = fCPUInfo[num].find(info);
if (i != fCPUInfo[num].end())
return i->second;
return "";
}