forked from JanekOstendorf/THOMAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatusInformation.cpp
197 lines (148 loc) · 3.98 KB
/
StatusInformation.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
// Die Header Datei
#include "StatusInformation.h"
// Eingabe-Ausgabe-Stream Header
#include <iostream>
// File-Stream header
#include <fstream>
// Enhält u.a die atoi-Funktion
#include <cstdlib>
// Standard Symbolische Konstanten und Typen
#include <unistd.h>
// Enthält das Vector-Element
#include <vector>
// Enthält die statvfs-Struktur => Festplatten informationen
#include <sys/statvfs.h>
// Enthält die Thread-Klasse
#include <thread>
using namespace THOMAS;
StatusInformation::StatusInformation()
{
}
void StatusInformation::CPUCaptureThread()
{
while(true)
{
// Wenn kein Client connectet ist, CPU nicht unnötig belasten
while(!_status)
usleep(50000);
// Zugriff von anderen Threads sperren
CPUMutex.lock();
// Prozessorauslastung abfragen und speichern
_CPUUsage = GetCPUUsage();
// Zugriff von anderen Threads erlauben
CPUMutex.unlock();
// 200ms warten
usleep(50000);
}
}
// Status ändern => Wenn Client connected
void StatusInformation::SetClientConnectStatus(bool status)
{
//Status setzten
_status = status;
}
std::vector<int> StatusInformation::GetMemoryInfo()
{
// Array erstellen
std::vector<int> memoryData(3);
// Werte ermitteln
long numPages = sysconf(_SC_PHYS_PAGES)/1024;
long pageSize = sysconf(_SC_PAGESIZE)/1024;
long freePages = sysconf( _SC_AVPHYS_PAGES)/1024;
// Werte setzten
memoryData[0] = (int) (numPages * pageSize);
memoryData[1] = (int) (pageSize * freePages);
memoryData[2] = (int) ((numPages * pageSize) - (pageSize * freePages));
// Ram-Infos als Array zurückgeben
return memoryData;
}
int StatusInformation::GetDiskSpace()
{
// Struktur mit Infos erstellen
struct statvfs buff;
// Informationen in Buffer laden
statvfs("/", &buff);
// Größe berechnen und in MB umwandeln
return (((double)buff.f_bavail * buff.f_bsize) / 1048576 );
}
int StatusInformation::GetCPUTemperature()
{
// Datei-Handler
std::ifstream CPUTemperatureFile;
// Input String
std::string input;
// Datei mit CPU Temperatur öffnen TODO: NR. an System anpassen. (0 = default)
CPUTemperatureFile.open("/sys/class/thermal/thermal_zone2/temp");
// Ist geöffnet?
if(!CPUTemperatureFile.is_open())
{
// Windoof?
std::cout << "Konnte Datei nicht öffnen!" << std::endl;
}
// Wert lesen
std::getline(CPUTemperatureFile, input);
// Wert zurück geben und durch 1000 Teilen
return atoi(input.c_str())/1000;
}
int StatusInformation::CalculateSum(std::vector<int> data, int start, int end)
{
// Summe initialisieren
int sum = 0;
// Array auslesen und addieren
for(int i = start; i < end; i++)
{
sum += data[i];
}
// Gibt die errechnete Summe zurück
return sum;
}
// Gibt die gespeicherte CPU Auslastung zurück.
float StatusInformation::GetCPUUsageSaved()
{
// CPU Last zurück geben
return _CPUUsage;
}
float StatusInformation::GetCPUUsage()
{
// Array mit Berechneten Werten
std::vector<int> calculationData(4);
// Werte einfügen = first
calculationData[0] = CalculateSum(GetCPUData(), 2, 9);
calculationData[1] = CalculateSum(GetCPUData(), 2, 5);
// CPU Last von 1 Sekunde
usleep(100000);
// Werte einfügen => second
calculationData[2] = CalculateSum(GetCPUData(), 2, 9);
calculationData[3] = CalculateSum(GetCPUData(), 2, 5);
// CPU Last berrechnen
float cpuLoad = ((float) (calculationData[3] - calculationData[1]) / (float) (calculationData[2] - calculationData[0])) * 100;
return cpuLoad;
}
std::vector<int> StatusInformation::GetCPUData()
{
// Neues Array mit CPU Informationen
std::vector<int> cpuData(9);
// Datei-Handler
std::ifstream cpuStats;
// Input String
std::string input;
// Datei mit CPU Infos öffnen
cpuStats.open("/proc/stat");
// Ist geöffnet?
if(!cpuStats.is_open())
{
// Windoof?
std::cout << "Konnte Datei nicht öffnen!" << std::endl;
}
// Daten auslesen
for(int i = 0; i < 9; i++)
{
// Daten abrufen und speichern
std::getline(cpuStats, input, ' ');
cpuData[i] = atoi(input.c_str());
}
// Datei schließen
cpuStats.close();
// CPU-Daten zurückgeben
return cpuData;
}