1 package com.guinetik.hexafun.examples.sysmon;
2
3 import java.util.function.Function;
4
5 import static com.guinetik.hexafun.examples.tui.Ansi.*;
6 import static com.guinetik.hexafun.examples.tui.Widgets.*;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public final class SysmonAdapters {
24
25 private SysmonAdapters() {}
26
27 private static final int BAR_WIDTH = 20;
28 private static final int BOX_WIDTH = 36;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public static final Function<SystemMetrics, String> TUI_ADAPTER = metrics -> {
46 StringBuilder sb = new StringBuilder();
47 String indent = " ";
48
49
50 sb.append(indent).append(color(BOX_TOP_LEFT + "─ System Monitor " +
51 repeat(BOX_HORIZONTAL, BOX_WIDTH - 19) + BOX_TOP_RIGHT, CYAN)).append("\n");
52
53
54 sb.append(indent).append(color(BOX_VERTICAL, CYAN))
55 .append(gauge("CPU ", metrics.cpu(), metrics.cpuWarning()))
56 .append(color(BOX_VERTICAL, CYAN)).append("\n");
57
58
59 sb.append(indent).append(color(BOX_VERTICAL, CYAN))
60 .append(gauge("MEM ", metrics.memory(), metrics.memoryWarning()))
61 .append(color(BOX_VERTICAL, CYAN)).append("\n");
62
63
64 sb.append(indent).append(color(BOX_VERTICAL, CYAN))
65 .append(gauge("DISK", metrics.disk(), metrics.diskWarning()))
66 .append(color(BOX_VERTICAL, CYAN)).append("\n");
67
68
69 sb.append(indent).append(color(BOX_BOTTOM_LEFT + repeat(BOX_HORIZONTAL, BOX_WIDTH - 2) +
70 BOX_BOTTOM_RIGHT, CYAN)).append("\n");
71
72 return sb.toString();
73 };
74
75
76
77
78 private static String gauge(String label, double percent, boolean warning) {
79 int filled = (int) ((BAR_WIDTH * percent) / 100);
80 int empty = BAR_WIDTH - filled;
81
82 String barColor = warning ? RED : GREEN;
83 String warnIcon = warning ? color(" ⚠", YELLOW) : " ";
84
85 return " " + color(label, BOLD) + " " +
86 color("[", DIM) +
87 color(repeat(BLOCK_FULL, filled), barColor) +
88 color(repeat(BLOCK_LIGHT, empty), BRIGHT_BLACK) +
89 color("]", DIM) +
90 color(String.format(" %3.0f%%", percent), warning ? RED : WHITE) +
91 warnIcon + " ";
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public static final Function<SystemMetrics, String> CLI_ADAPTER = metrics ->
108 String.format("cpu: %.0f%%\nmem: %.0f%%\ndisk: %.0f%%\n",
109 metrics.cpu(), metrics.memory(), metrics.disk());
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public static final Function<SystemMetrics, String> JSON_ADAPTER = metrics -> {
128 StringBuilder warnings = new StringBuilder("[");
129 boolean first = true;
130 if (metrics.cpuWarning()) {
131 warnings.append("\"cpu\"");
132 first = false;
133 }
134 if (metrics.memoryWarning()) {
135 if (!first) warnings.append(", ");
136 warnings.append("\"memory\"");
137 first = false;
138 }
139 if (metrics.diskWarning()) {
140 if (!first) warnings.append(", ");
141 warnings.append("\"disk\"");
142 }
143 warnings.append("]");
144
145 return String.format(
146 "{\n \"cpu\": %.1f,\n \"memory\": %.1f,\n \"disk\": %.1f,\n \"warnings\": %s\n}\n",
147 metrics.cpu(), metrics.memory(), metrics.disk(), warnings
148 );
149 };
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public static final Function<SystemMetrics, String> PROMETHEUS_ADAPTER = metrics -> {
171 StringBuilder sb = new StringBuilder();
172
173 sb.append("# HELP system_cpu_percent Current CPU usage percentage\n");
174 sb.append("# TYPE system_cpu_percent gauge\n");
175 sb.append(String.format("system_cpu_percent %.1f\n", metrics.cpu()));
176
177 sb.append("# HELP system_memory_percent Current memory usage percentage\n");
178 sb.append("# TYPE system_memory_percent gauge\n");
179 sb.append(String.format("system_memory_percent %.1f\n", metrics.memory()));
180
181 sb.append("# HELP system_disk_percent Current disk usage percentage\n");
182 sb.append("# TYPE system_disk_percent gauge\n");
183 sb.append(String.format("system_disk_percent %.1f\n", metrics.disk()));
184
185 return sb.toString();
186 };
187 }