]>
git.argeo.org Git - gpl/argeo-slc.git/blob - lib/linux/org.argeo.slc.systemd/src/org/argeo/slc/systemd/dbus/ServiceStatistics.java
1 package org
.argeo
.slc
.systemd
.dbus
;
3 import static java
.lang
.System
.Logger
.Level
.DEBUG
;
4 import static java
.lang
.System
.Logger
.Level
.INFO
;
5 import static java
.lang
.System
.Logger
.Level
.WARNING
;
7 import java
.io
.IOException
;
9 import java
.lang
.System
.Logger
;
10 import java
.nio
.charset
.StandardCharsets
;
11 import java
.nio
.file
.Files
;
12 import java
.nio
.file
.Path
;
13 import java
.nio
.file
.Paths
;
14 import java
.nio
.file
.StandardOpenOption
;
15 import java
.time
.Instant
;
16 import java
.time
.ZoneOffset
;
17 import java
.time
.format
.DateTimeFormatter
;
19 import org
.argeo
.cms
.util
.CsvWriter
;
20 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
22 import de
.thjom
.java
.systemd
.Manager
;
23 import de
.thjom
.java
.systemd
.Service
;
24 import de
.thjom
.java
.systemd
.Systemd
;
25 import de
.thjom
.java
.systemd
.types
.UnitType
;
27 /** Gathers statistics about the runnign process, if it is a systemd unit. */
28 public class ServiceStatistics
{
29 private final static Logger logger
= System
.getLogger(ServiceStatistics
.class.getName());
31 private Manager manager
;
33 private String unitName
;
34 private Service service
;
36 private long frequency
= 60 * 1000;
39 final long pid
= ProcessHandle
.current().pid();
41 manager
= Systemd
.get().getManager();
43 for (UnitType unitType
: manager
.listUnits()) {
44 if (unitType
.isService()) {
45 Service s
= manager
.getService(unitType
.getUnitName());
46 if (s
.getMainPID() == pid
) {
48 unitName
= unitType
.getUnitName();
49 logger
.log(INFO
, "Systemd service found for pid " + pid
+ ": " + unitName
);
54 } catch (DBusException e
) {
55 logger
.log(WARNING
, "Cannot connect to systemd", e
);
58 if (service
== null) {
59 logger
.log(DEBUG
, () -> "No systemd service found for pid " + pid
+ ", disconnecting from DBus...");
69 if (manager
!= null) {
76 protected void collectStatistics() {
77 // standard systemd property
78 String logsDirectory
= System
.getenv("LOGS_DIRECTORY");
79 if (logsDirectory
== null) {
80 logsDirectory
= System
.getProperty("user.dir");
82 // MainPID,CPUUsageNSec,MemoryCurrent,IPIngressBytes,IPEgressBytes,IOReadBytes,IOWriteBytes,TasksCurrent
83 Path basePath
= Paths
.get(logsDirectory
);
85 logger
.log(DEBUG
, () -> "Writing statistics for " + unitName
+ " to " + basePath
);
87 while (manager
!= null) {
88 String dateSuffix
= Instant
.now().atOffset(ZoneOffset
.UTC
).format(DateTimeFormatter
.ISO_LOCAL_DATE
);
90 Path csvPath
= basePath
.resolve("statistics-" + unitName
+ "-" + dateSuffix
+ ".csv");
91 boolean writeHeader
= !Files
.exists(csvPath
);
92 try (Writer writer
= Files
.newBufferedWriter(csvPath
, StandardCharsets
.UTF_8
,
93 StandardOpenOption
.APPEND
)) {
94 CsvWriter csvWriter
= new CsvWriter(writer
);
96 if (writeHeader
)// header
97 csvWriter
.writeLine("CurrentTimeMillis", "CPUUsageNSec", "MemoryCurrent", "IPIngressBytes",
98 "IPEgressBytes", "IOReadBytes", "IOWriteBytes", "TasksCurrent");
100 // TODO better synchronise with stop
101 csvWriter
.writeLine(System
.currentTimeMillis(), service
.getCPUUsageNSec(),
102 service
.getMemoryCurrent(), service
.getIPIngressBytes(), service
.getIPEgressBytes(),
103 service
.getIOReadBytes(), service
.getIOWriteBytes(), service
.getTasksCurrent());
105 Thread
.sleep(frequency
);
107 } catch (IOException e
) {
108 throw new IllegalStateException("Cannot collect statistics", e
);
109 } catch (InterruptedException e
) {
110 logger
.log(WARNING
, "Statistics collection interrupted for " + unitName
);
115 public static void main(String
[] args
) throws Exception
{
117 Systemd systemd
= Systemd
.get();
118 Service service
= systemd
.getManager().getService("ipsec.service");
119 System
.out
.println(service
.getCPUUsageNSec());
121 for (UnitType unitType
: systemd
.getManager().listUnits()) {
122 if (unitType
.isService()) {
123 System
.out
.println(unitType
.getUnitName());
128 Systemd
.disconnect();