aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/SystemInfo.java
blob: 1497362a62b86752bb0a2b1866f21bf79e9663b4 (plain)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about;

import java.io.File;
import java.io.IOException;
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Locale;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;

public class SystemInfo {
    private static NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH));
    private static NumberFormat fmtDec = new DecimalFormat("###,###.##", new DecimalFormatSymbols(Locale.ENGLISH));
    private static NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH));
    private static OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    protected static boolean showMemoryPools = false;

    public static String getMdSalVersion(String def) {
        return getMdSalVersion("", def);
    }

    public static String getYangToolsVersion(String def) {
        return getYangToolsVersion("", def);
    }

    public static String getMdSalVersion(String baseOdlDirectory, String def) {
        return getFeatureVersionByFolder(baseOdlDirectory, "system/org/opendaylight/mdsal/mdsal-binding-api/", def);
    }

    public static String getYangToolsVersion(String baseOdlDirectory, String def) {
        return getFeatureVersionByFolder(baseOdlDirectory, "system/org/opendaylight/yangtools/odl-yangtools-common/",
                def);
    }

    private static String getFeatureVersionByFolder(String baseOdlDirectory, String dir, String def) {
        final String regex = "^[0-9]+\\.[0-9]+\\.[0-9]+(-SNAPSHOT)?$";
        Stream<Path> entries = null;
        try {
            if (baseOdlDirectory != null && baseOdlDirectory.length() > 0 && !baseOdlDirectory.endsWith("/")) {
                baseOdlDirectory += "/";
            }
            entries = Files.list(new File(baseOdlDirectory + dir).toPath());
        } catch (IOException e) {

        }
        if (entries == null) {
            return def;
        }
        final Pattern pattern = Pattern.compile(regex);

        Iterator<Path> it = entries.iterator();
        Path p;
        File f;
        while (it.hasNext()) {
            p = it.next();
            f = p.toFile();
            if (f.isDirectory()) {
                final Matcher matcher = pattern.matcher(f.getName().toString());
                if (matcher.find()) {
                    def = matcher.group(0);
                    break;
                }
            }
        }
        entries.close();
        return def;
    }

    public static String get() throws Exception {
        StringBuilder sb = new StringBuilder();
        int maxNameLen;

        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        ThreadMXBean threads = ManagementFactory.getThreadMXBean();
        MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
        ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean();

        //
        // print Karaf informations
        //
        maxNameLen = 25;
        sb.append("Karaf\n");
        printValue(sb, "Karaf version", maxNameLen, System.getProperty("karaf.version"));
        printValue(sb, "Karaf home", maxNameLen, System.getProperty("karaf.home"));
        printValue(sb, "Karaf base", maxNameLen, System.getProperty("karaf.base"));
        String osgi = getOsgiFramework();
        if (osgi != null) {
            printValue(sb, "OSGi Framework", maxNameLen, osgi);
        }

        sb.append("JVM\n");
        printValue(sb, "Java Virtual Machine", maxNameLen, runtime.getVmName() + " version " + runtime.getVmVersion());
        printValue(sb, "Version", maxNameLen, System.getProperty("java.version"));
        printValue(sb, "Vendor", maxNameLen, runtime.getVmVendor());
        printValue(sb, "Pid", maxNameLen, getPid());
        printValue(sb, "Uptime", maxNameLen, printDuration(runtime.getUptime()));
        try {
            Class<?> sunOS = Class.forName("com.sun.management.OperatingSystemMXBean");
            printValue(sb, "Process CPU time", maxNameLen,
                    printDuration(getValueAsLong(sunOS, "getProcessCpuTime") / 1000000.0));
            printValue(sb, "Process CPU load", maxNameLen, fmtDec.format(getValueAsDouble(sunOS, "getProcessCpuLoad")));
            printValue(sb, "System CPU load", maxNameLen, fmtDec.format(getValueAsDouble(sunOS, "getSystemCpuLoad")));
        } catch (Throwable t) {
        }
        try {
            Class<?> unixOS = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
            printValue(sb, "Open file descriptors", maxNameLen,
                    printLong(getValueAsLong(unixOS, "getOpenFileDescriptorCount")));
            printValue(sb, "Max file descriptors", maxNameLen,
                    printLong(getValueAsLong(unixOS, "getMaxFileDescriptorCount")));
        } catch (Throwable t) {
        }
        printValue(sb, "Total compile time", maxNameLen,
                printDuration(ManagementFactory.getCompilationMXBean().getTotalCompilationTime()));

        sb.append("Threads\n");
        printValue(sb, "Live threads", maxNameLen, Integer.toString(threads.getThreadCount()));
        printValue(sb, "Daemon threads", maxNameLen, Integer.toString(threads.getDaemonThreadCount()));
        printValue(sb, "Peak", maxNameLen, Integer.toString(threads.getPeakThreadCount()));
        printValue(sb, "Total started", maxNameLen, Long.toString(threads.getTotalStartedThreadCount()));

        sb.append("Memory\n");
        printValue(sb, "Current heap size", maxNameLen, printSizeInKb(mem.getHeapMemoryUsage().getUsed()));
        printValue(sb, "Maximum heap size", maxNameLen, printSizeInKb(mem.getHeapMemoryUsage().getMax()));
        printValue(sb, "Committed heap size", maxNameLen, printSizeInKb(mem.getHeapMemoryUsage().getCommitted()));
        printValue(sb, "Pending objects", maxNameLen, Integer.toString(mem.getObjectPendingFinalizationCount()));
        for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
            String val = "Name = '" + gc.getName() + "', Collections = " + gc.getCollectionCount() + ", Time = "
                    + printDuration(gc.getCollectionTime());
            printValue(sb, "Garbage collector", maxNameLen, val);
        }

        //		if (showMemoryPools) {
        //			List<MemoryPoolMXBean> memoryPools = ManagementFactory.getMemoryPoolMXBeans();
        //			sb.append("Memory Pools\n");
        //			printValue(sb, "Total Memory Pools", maxNameLen, printLong(memoryPools.size()));
        //			String spaces4 = "   ";
        //			for (MemoryPoolMXBean pool : memoryPools) {
        //				String name = pool.getName();
        //				MemoryType type = pool.getType();
        //				printValue(sb, spaces4 + "Pool (" + type + ")", maxNameLen, name);
        //
        //				// PeakUsage/CurrentUsage
        //				MemoryUsage peakUsage = pool.getPeakUsage();
        //				MemoryUsage usage = pool.getUsage();
        //
        //				if (usage != null && peakUsage != null) {
        //					long init = peakUsage.getInit();
        //					long used = peakUsage.getUsed();
        //					long committed = peakUsage.getCommitted();
        //					long max = peakUsage.getMax();
        //					sb.append(spaces4 + spaces4 + "Peak Usage\n");
        //					printValue(sb, spaces4 + spaces4 + spaces4 + "init", maxNameLen, printLong(init));
        //					printValue(sb, spaces4 + spaces4 + spaces4 + "used", maxNameLen, printLong(used));
        //					printValue(sb, spaces4 + spaces4 + spaces4 + "committed", maxNameLen, printLong(committed));
        //					printValue(sb, spaces4 + spaces4 + spaces4 + "max", maxNameLen, printLong(max));
        //
        //					init = usage.getInit();
        //					used = usage.getUsed();
        //					committed = usage.getCommitted();
        //					max = usage.getMax();
        //					sb.append(spaces4 + spaces4 + "Current Usage\n");
        //					printValue(sb, spaces4 + spaces4 + spaces4 + "init", maxNameLen, printLong(init));
        //					printValue(sb, spaces4 + spaces4 + spaces4 + "used", maxNameLen, printLong(used));
        //					printValue(sb, spaces4 + spaces4 + spaces4 + "committed", maxNameLen, printLong(committed));
        //					printValue(sb, spaces4 + spaces4 + spaces4 + "max", maxNameLen, printLong(max));
        //				}
        //			}
        //		}

        sb.append("Classes\n");
        printValue(sb, "Current classes loaded", maxNameLen, printLong(cl.getLoadedClassCount()));
        printValue(sb, "Total classes loaded", maxNameLen, printLong(cl.getTotalLoadedClassCount()));
        printValue(sb, "Total classes unloaded", maxNameLen, printLong(cl.getUnloadedClassCount()));

        sb.append("Operating system\n");
        printValue(sb, "Name", maxNameLen, os.getName() + " version " + os.getVersion());
        printValue(sb, "Architecture", maxNameLen, os.getArch());
        printValue(sb, "Processors", maxNameLen, Integer.toString(os.getAvailableProcessors()));
        try {
            printValue(sb, "Total physical memory", maxNameLen,
                    printSizeInKb(getSunOsValueAsLong(os, "getTotalPhysicalMemorySize")));
            printValue(sb, "Free physical memory", maxNameLen,
                    printSizeInKb(getSunOsValueAsLong(os, "getFreePhysicalMemorySize")));
            printValue(sb, "Committed virtual memory", maxNameLen,
                    printSizeInKb(getSunOsValueAsLong(os, "getCommittedVirtualMemorySize")));
            printValue(sb, "Total swap space", maxNameLen,
                    printSizeInKb(getSunOsValueAsLong(os, "getTotalSwapSpaceSize")));
            printValue(sb, "Free swap space", maxNameLen,
                    printSizeInKb(getSunOsValueAsLong(os, "getFreeSwapSpaceSize")));
        } catch (Throwable t) {
        }
        return sb.toString();
    }

    private static String getPid() {
        // In Java 9 the new process API can be used:
        // long pid = ProcessHandle.current().getPid();
        String name = ManagementFactory.getRuntimeMXBean().getName();
        String[] parts = name.split("@");
        return parts[0];
    }

    private static long getSunOsValueAsLong(OperatingSystemMXBean os, String name) throws Exception {
        Method mth = os.getClass().getMethod(name);
        return (Long) mth.invoke(os);
    }

    private static long getValueAsLong(Class<?> osImpl, String name) throws Exception {
        if (osImpl.isInstance(os)) {
            Method mth = osImpl.getMethod(name);
            return (Long) mth.invoke(os);
        }
        return -1;
    }

    private static double getValueAsDouble(Class<?> osImpl, String name) throws Exception {
        if (osImpl.isInstance(os)) {
            Method mth = osImpl.getMethod(name);
            return (Double) mth.invoke(os);
        }
        return -1;
    }

    private static String printLong(long i) {
        return fmtI.format(i);
    }

    private static String printSizeInKb(double size) {
        return fmtI.format((long) (size / 1024)) + " kbytes";
    }

    protected static String printDuration(double uptime) {
        uptime /= 1000;
        if (uptime < 60) {
            return fmtD.format(uptime) + " seconds";
        }
        uptime /= 60;
        if (uptime < 60) {
            long minutes = (long) uptime;
            String s = fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
            return s;
        }
        uptime /= 60;
        if (uptime < 24) {
            long hours = (long) uptime;
            long minutes = (long) ((uptime - hours) * 60);
            String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
            if (minutes != 0) {
                s += " " + fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
            }
            return s;
        }
        uptime /= 24;
        long days = (long) uptime;
        long hours = (long) ((uptime - days) * 24);
        String s = fmtI.format(days) + (days > 1 ? " days" : " day");
        if (hours != 0) {
            s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
        }
        return s;
    }

    static void printSysValue(StringBuilder sb, String prop, int pad) {
        printValue(sb, prop, pad, System.getProperty(prop));
    }

    static void printValue(StringBuilder sb, String name, int pad, String value) {
        sb.append("  " + // SimpleAnsi.INTENSITY_BOLD +
                name + // SimpleAnsi.INTENSITY_NORMAL +
                spaces(pad - name.length()) + "   " + value + "\n");
    }

    static String spaces(int nb) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < nb; i++) {
            sb.append(' ');
        }
        return sb.toString();
    }

    static String getOsgiFramework() {
        try {
            Callable<String> call = new Callable<String>() {
                @Override
                public String call() throws Exception {
                    BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
                    Bundle sysBundle = context.getBundle(0);
                    return sysBundle.getSymbolicName() + "-" + sysBundle.getVersion();
                }
            };
            return call.call();
        } catch (Throwable t) {
            // We're not in OSGi, just safely return null
            return null;
        }
    }
}