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
|
--- src/data_provider/src/sysInfoFreeBSD.cpp 2025-01-15 06:26:54.000000000 -0800
+++ src/data_provider/src/sysInfoFreeBSD.cpp 2025-02-17 14:38:11.834720000 -0800
@@ -11,6 +11,7 @@
#include "sysInfo.hpp"
#include "cmdHelper.h"
#include "stringHelper.h"
+#include "timeHelper.h"
#include "osinfo/sysOsParsers.h"
#include <sys/sysctl.h>
#include <sys/vmmeter.h>
@@ -19,12 +20,13 @@
static void getMemory(nlohmann::json& info)
{
+ constexpr auto vmFree{"vm.stats.vm.v_free_count"};
+ constexpr auto vmInactive{"vm.stats.vm.v_inactive_count"};
constexpr auto vmPageSize{"vm.stats.vm.v_page_size"};
- constexpr auto vmTotal{"vm.vmtotal"};
+ constexpr auto vmTotal{"hw.physmem"};
uint64_t ram{0};
- const std::vector<int> mib{CTL_HW, HW_PHYSMEM};
size_t len{sizeof(ram)};
- auto ret{sysctl(const_cast<int*>(mib.data()), mib.size(), &ram, &len, nullptr, 0)};
+ auto ret{sysctlbyname(vmTotal, &ram, &len, nullptr, 0)};
if (ret)
{
@@ -52,11 +54,23 @@
};
}
- struct vmtotal vmt {};
+ uint64_t freeMem{0};
+ len = sizeof(freeMem);
+ ret = sysctlbyname(vmFree, &freeMem, &len, nullptr, 0);
- len = sizeof(vmt);
+ if (ret)
+ {
+ throw std::system_error
+ {
+ ret,
+ std::system_category(),
+ "Error reading free memory size."
+ };
+ }
- ret = sysctlbyname(vmTotal, &vmt, &len, nullptr, 0);
+ uint64_t inactiveMem{0};
+ len = sizeof(inactiveMem);
+ ret = sysctlbyname(vmInactive, &inactiveMem, &len, nullptr, 0);
if (ret)
{
@@ -64,11 +78,11 @@
{
ret,
std::system_category(),
- "Error reading total memory."
+ "Error reading inactive memory size."
};
}
- const auto ramFree{(vmt.t_free * pageSize) / KByte};
+ const auto ramFree{(freeMem + inactiveMem) * pageSize / KByte};
info["ram_free"] = ramFree;
info["ram_usage"] = 100 - (100 * ramFree / ramTotal);
}
@@ -184,8 +198,12 @@
nlohmann::json SysInfo::getProcessesInfo() const
{
- // Currently not supported for this OS
- return nlohmann::json {};
+ nlohmann::json ret;
+ getProcessesInfo([&ret](nlohmann::json & data)
+ {
+ ret.push_back(data);
+ });
+ return ret;
}
nlohmann::json SysInfo::getOsInfo() const
@@ -196,11 +214,12 @@
if (!spParser->parseUname(Utils::exec("uname -r"), ret))
{
- ret["os_name"] = "BSD";
ret["os_platform"] = "bsd";
ret["os_version"] = UNKNOWN_VALUE;
}
+ ret["os_name"] = "FreeBSD";
+
if (uname(&uts) >= 0)
{
ret["sysname"] = uts.sysname;
@@ -215,18 +234,145 @@
nlohmann::json SysInfo::getPorts() const
{
- // Currently not supported for this OS.
- return nlohmann::json {};
+ const auto query{Utils::exec(R"(sockstat -46qs)")};
+
+ /* USER COMMAND PID FD PROTO LOCAL_ADDRESS FOREIGN_ADDRESS PATH_STATE CONN_STATE */
+
+ nlohmann::json ports {};
+
+ if (!query.empty())
+ {
+ const auto lines{Utils::split(Utils::trimToOneSpace(query), '\n')};
+
+ for (const auto& line : lines)
+ {
+ std::string localip = "";
+ std::string localport = "";
+ std::string remoteip = "";
+ std::string remoteport = "";
+ std::string statedata = "";
+
+ const auto data{Utils::split(line, ' ')};
+ auto localdata{Utils::split(data[5], ':')};
+ auto remotedata{Utils::split(data[6], ':')};
+
+ localip = localdata[0];
+ localport = localdata[1];
+ remoteip = remotedata[0];
+ remoteport = remotedata[1];
+
+ if((data[4] != "udp4") && (data[4] != "udp6") && (data[4] != "udp46")) {
+ statedata = Utils::toLowerCase(data[7]);
+ }
+
+ if(statedata == "listen") {
+ statedata = "listening";
+ }
+
+ if(localdata.size() == 4) {
+ localip = localdata[0] + ":"+ localdata[1] + ":" + localdata[2];
+ localport = localdata[3];
+ }
+
+ if(localip == "*") {
+ if((data[4] == "tcp6") || (data[4] == "udp6")) {
+ localip = "0:0:0:0:0:0:0:0";
+ } else if((data[4] == "tcp4") || (data[4] == "udp4")) {
+ localip = "0.0.0.0";
+ }
+ }
+
+ if(localport == "*") {
+ localport = "0";
+ }
+
+ if(remotedata.size() == 4) {
+ remoteip = remotedata[0] + ":"+ remotedata[1] + ":" + remotedata[2];
+ remoteport = remotedata[3];
+ }
+
+ if(remoteport == "*") {
+ remoteip = "";
+ remoteport = "0";
+ }
+
+ if(data[0] != "?") {
+ nlohmann::json port {};
+ port["protocol"] = data[4];
+ port["local_ip"] = localip;
+ port["local_port"] = localport;
+ port["remote_ip"] = remoteip;
+ port["remote_port"] = remoteport;
+ port["tx_queue"] = 0;
+ port["rx_queue"] = 0;
+ port["inode"] = data[3];
+ port["state"] = statedata;
+ port["pid"] = data[2];
+ port["process"] = data[1];
+
+ ports.push_back(port);
+ }
+ }
+ }
+
+ return ports;
}
-void SysInfo::getProcessesInfo(std::function<void(nlohmann::json&)> /*callback*/) const
+void SysInfo::getProcessesInfo(std::function<void(nlohmann::json&)> callback) const
{
- // Currently not supported for this OS.
+ const auto query{Utils::exec(R"(ps -ax -w -o pid,comm,state,ppid,usertime,systime,user,ruser,svuid,group,rgroup,svgid,pri,nice,ssiz,vsz,rss,pmem,etimes,sid,pgid,tpgid,tty,cpu,nlwp,args --libxo json)")};
+
+ if (!query.empty())
+ {
+ nlohmann::json psjson;
+ psjson = nlohmann::json::parse(query);
+ auto &processes = psjson["process-information"]["process"];
+
+ for(auto &process : processes) {
+ std::string user_time{""};
+ std::string system_time{""};
+
+ user_time = process["user-time"].get<std::string>();
+ system_time = process["system-time"].get<std::string>();
+
+ nlohmann::json jsProcessInfo{};
+ jsProcessInfo["pid"] = process["pid"].get<std::string>();
+ jsProcessInfo["name"] = process["command"].get<std::string>();
+ jsProcessInfo["state"] = process["state"].get<std::string>();
+ jsProcessInfo["ppid"] = process["ppid"].get<std::string>();
+ jsProcessInfo["utime"] = Utils::timeToSeconds(user_time);
+ jsProcessInfo["stime"] = Utils::timeToSeconds(system_time);
+ jsProcessInfo["cmd"] = process["command"].get<std::string>();
+ jsProcessInfo["argvs"] = process["arguments"].get<std::string>();
+ jsProcessInfo["euser"] = process["user"].get<std::string>();
+ jsProcessInfo["ruser"] = process["real-user"].get<std::string>();
+ jsProcessInfo["suser"] = process["saved-uid"].get<std::string>();
+ jsProcessInfo["egroup"] = process["group"].get<std::string>();
+ jsProcessInfo["rgroup"] = process["real-group"].get<std::string>();
+ jsProcessInfo["sgroup"] = process["saved-gid"].get<std::string>();
+ jsProcessInfo["fgroup"] = process["group"].get<std::string>();
+ jsProcessInfo["priority"] = process["priority"].get<std::string>();
+ jsProcessInfo["nice"] = process["nice"].get<std::string>();
+ jsProcessInfo["size"] = process["stack-size"].get<std::string>();
+ jsProcessInfo["vm_size"] = process["virtual-size"].get<std::string>();
+ jsProcessInfo["resident"] = process["rss"].get<std::string>();
+ //jsProcessInfo["share"] = process["percent-memory"].get<std::string>();
+ jsProcessInfo["start_time"] = process["elapsed-times"].get<std::string>() == "-" ? "0" : process["elapsed-times"].get<std::string>();
+ jsProcessInfo["pgrp"] = process["process-group"].get<std::string>();
+ jsProcessInfo["session"] = process["sid"].get<std::string>();
+ jsProcessInfo["tgid"] = process["terminal-process-gid"].get<std::string>();
+ //jsProcessInfo["tty"] = process["tty"].get<std::string>(); // this field should be TEXT into local.db
+ jsProcessInfo["processor"] = process["on-cpu"].get<std::string>();
+ jsProcessInfo["nlwp"] = process["threads"].get<std::string>();
+
+ callback(jsProcessInfo);
+ }
+ }
}
void SysInfo::getPackages(std::function<void(nlohmann::json&)> callback) const
{
- const auto query{Utils::exec(R"(pkg query -a "%n|%m|%v|%q|%c")")};
+ const auto query{Utils::exec(R"(pkg query -a "%n|%m|%v|%q|%c|%sb|%t|%R|%o")")};
if (!query.empty())
{
@@ -235,18 +381,22 @@
for (const auto& line : lines)
{
const auto data{Utils::split(line, '|')};
+ const auto archdata{Utils::split(data[3], ':')};
+ const auto sectiondata{Utils::split(data[8], '/')};
+
nlohmann::json package;
package["name"] = data[0];
package["vendor"] = data[1];
package["version"] = data[2];
- package["install_time"] = UNKNOWN_VALUE;
+ package["install_time"] = data[6];
package["location"] = UNKNOWN_VALUE;
- package["architecture"] = data[3];
+ package["architecture"] = archdata[2];
package["groups"] = UNKNOWN_VALUE;
package["description"] = data[4];
- package["size"] = 0;
+ package["size"] = data[5];
package["priority"] = UNKNOWN_VALUE;
- package["source"] = UNKNOWN_VALUE;
+ package["source"] = data[7];
+ package["section"] = sectiondata[0];
package["format"] = "pkg";
// The multiarch field won't have a default value
|