]> git.argeo.org Git - gpl/argeo-slc.git/blob - lib/linux/org.argeo.slc.systemd/src/org/argeo/slc/systemd/dbus/ServiceStatistics.java
Remove factory
[gpl/argeo-slc.git] / lib / linux / org.argeo.slc.systemd / src / org / argeo / slc / systemd / dbus / ServiceStatistics.java
1 package org.argeo.slc.systemd.dbus;
2
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;
6
7 import java.io.IOException;
8 import java.io.Writer;
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;
18
19 import org.argeo.cms.util.CsvWriter;
20 import org.freedesktop.dbus.exceptions.DBusException;
21
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;
26
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());
30
31 private Manager manager;
32
33 private String unitName;
34 private Service service;
35
36 private long frequency = 60 * 1000;
37
38 public void start() {
39 final long pid = ProcessHandle.current().pid();
40 try {
41 manager = Systemd.get().getManager();
42 // find own service
43 for (UnitType unitType : manager.listUnits()) {
44 if (unitType.isService()) {
45 Service s = manager.getService(unitType.getUnitName());
46 if (s.getMainPID() == pid) {
47 service = s;
48 unitName = unitType.getUnitName();
49 logger.log(INFO, "Systemd service found for pid " + pid + ": " + unitName);
50 }
51 }
52 }
53
54 } catch (DBusException e) {
55 logger.log(WARNING, "Cannot connect to systemd", e);
56 }
57
58 if (service == null) {
59 logger.log(DEBUG, () -> "No systemd service found for pid " + pid + ", disconnecting from DBus...");
60 manager = null;
61 Systemd.disconnect();
62 } else {
63 // start collecting
64 collectStatistics();
65 }
66 }
67
68 public void stop() {
69 if (manager != null) {
70 Systemd.disconnect();
71 manager = null;
72 notifyAll();
73 }
74 }
75
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");
81 }
82 // MainPID,CPUUsageNSec,MemoryCurrent,IPIngressBytes,IPEgressBytes,IOReadBytes,IOWriteBytes,TasksCurrent
83 Path basePath = Paths.get(logsDirectory);
84
85 logger.log(DEBUG, () -> "Writing statistics for " + unitName + " to " + basePath);
86 try {
87 while (manager != null) {
88 String dateSuffix = Instant.now().atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE);
89
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);
95
96 if (writeHeader)// header
97 csvWriter.writeLine("CurrentTimeMillis", "CPUUsageNSec", "MemoryCurrent", "IPIngressBytes",
98 "IPEgressBytes", "IOReadBytes", "IOWriteBytes", "TasksCurrent");
99
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());
104 }
105 Thread.sleep(frequency);
106 }
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);
111 }
112
113 }
114
115 public static void main(String[] args) throws Exception {
116 try {
117 Systemd systemd = Systemd.get();
118 Service service = systemd.getManager().getService("ipsec.service");
119 System.out.println(service.getCPUUsageNSec());
120
121 for (UnitType unitType : systemd.getManager().listUnits()) {
122 if (unitType.isService()) {
123 System.out.println(unitType.getUnitName());
124 }
125 }
126
127 } finally {
128 Systemd.disconnect();
129 }
130 }
131
132 }