isundil пре 4 година
родитељ
комит
bcaf2b5985

+ 5 - 5
app/src/main/java/info/knacki/prometheusandroidexporter/CollectorManager.java

@@ -9,7 +9,7 @@ import java.util.Map;
 public class CollectorManager {
     private static CollectorManager sInstance = null;
     private final HashMap<String, ICollector> fCollectors;
-    private HashMap<String, Collection<CollectorValue>> fValues;
+    private HashMap<String, Collection<CollectorType.CollectorValue>> fValues;
 
     private CollectorManager(){
         fCollectors = new HashMap<>();
@@ -20,7 +20,7 @@ public class CollectorManager {
     }
 
     public void tick(Context ctx) {
-        HashMap<String, Collection<CollectorValue>> values = new HashMap<>();
+        HashMap<String, Collection<CollectorType.CollectorValue>> values = new HashMap<>();
 
         for (HashMap.Entry<String, ICollector> i : fCollectors.entrySet()) {
             values.put(i.getKey(), i.getValue().ReadValues(ctx));
@@ -40,12 +40,12 @@ public class CollectorManager {
     }
 
     public interface Runnable {
-        void Run(CollectorValue value);
+        void Run(CollectorType.CollectorValue value);
     }
 
     public void forEach(Runnable action) {
-        for (Map.Entry<String, Collection<CollectorValue>> i: fValues.entrySet()) {
-            for (CollectorValue j: i.getValue())
+        for (Map.Entry<String, Collection<CollectorType.CollectorValue>> i: fValues.entrySet()) {
+            for (CollectorType.CollectorValue j: i.getValue())
                 action.Run(j);
         }
     }

+ 75 - 0
app/src/main/java/info/knacki/prometheusandroidexporter/CollectorType.java

@@ -0,0 +1,75 @@
+package info.knacki.prometheusandroidexporter;
+
+import androidx.annotation.NonNull;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class CollectorType {
+    public final String fName;
+    public final String fHelp;
+    public final Type fType;
+
+    public static class Type {
+        public final String fName;
+        private Type(String name) { this.fName = name; }
+
+        @Override
+        public String toString() {
+            return fName;
+        }
+
+        public final static Type COUNTER = new Type("counter");
+        public final static Type GAUGE = new Type("gauge");
+        public final static Type SUMMARY = new Type("summary");
+    }
+
+    public CollectorType(String name, String help, Type type) {
+        fName = "android_" + name;
+        fHelp = help;
+        fType = type;
+    }
+
+    public class CollectorValue {
+        private String valueAsString;
+        private Map<String, String> fParameters = new HashMap<>();
+
+        public String GetKey() {
+            StringBuilder result = new StringBuilder(fName);
+            if (!fParameters.isEmpty()) {
+                result.append("{");
+                int count = fParameters.size();
+                for (Map.Entry<String, String> i : fParameters.entrySet()) {
+                    result.append(i.getKey()).append("=\"").append(i.getValue()).append("\"");
+                    if (--count != 0)
+                        result.append(",");
+                }
+                result.append("}");
+            }
+            return result.toString();
+        }
+
+        public CollectorType GetType() {
+            return CollectorType.this;
+        }
+
+        public CollectorValue SetValue(double value) {
+            valueAsString = Double.toString(value);
+            return this;
+        }
+        public CollectorValue SetValue(int value) {
+            valueAsString = Integer.toString(value);
+            return this;
+        }
+
+        public void AddParameter(String key, String val) {
+            fParameters.put(key, val);
+        }
+
+        @NonNull
+        @Override
+        public String toString() {
+            return valueAsString;
+        }
+    }
+}

+ 0 - 49
app/src/main/java/info/knacki/prometheusandroidexporter/CollectorValue.java

@@ -1,49 +0,0 @@
-package info.knacki.prometheusandroidexporter;
-
-import androidx.annotation.NonNull;
-
-public class CollectorValue {
-    public static class Type {
-        public final String fName;
-        private Type(String name) { this.fName = name; }
-
-        public final static Type COUNTER = new Type("counter");
-        public final static Type GAUGE = new Type("gauge");
-        public final static Type SUMMARY = new Type("summary");
-        public final static Type STRING = new Type("string"); // FIXME legit ?
-    }
-
-    public final String fName;
-    public final String fHelp;
-    public final Type fType;
-    private String valueAsString;
-
-    public CollectorValue(String name, String help, Type type) {
-        fName = name;
-        fHelp = help;
-        fType = type;
-    }
-
-    public CollectorValue(CollectorValue other) {
-        fName = other.fName;
-        fHelp = other.fHelp;
-        fType = other.fType;
-    }
-
-    public void SetValue(String value) {
-        valueAsString = value;
-    }
-
-    public void SetValue(double value) {
-        valueAsString = Double.toString(value);
-    }
-    public void SetValue(int value) {
-        valueAsString = Integer.toString(value);
-    }
-
-    @NonNull
-    @Override
-    public String toString() {
-        return valueAsString;
-    }
-}

+ 5 - 5
app/src/main/java/info/knacki/prometheusandroidexporter/HttpService.java

@@ -16,7 +16,7 @@ import java.util.logging.Logger;
 
 public class HttpService {
     public final static Logger gLogger = Logger.getLogger(HttpService.class.getName());
-    public static final short PORT = 8080;
+    public static final short PORT = 9100;
     private static final HttpService gService = new HttpService();
     private HttpServer fServer = null;
 
@@ -84,10 +84,10 @@ public class HttpService {
         final StringBuilder os = new StringBuilder();
         CollectorManager.GetInstance().forEach(new CollectorManager.Runnable() {
             @Override
-            public void Run(CollectorValue i) {
-                os.append("# HELP ").append(i.fName).append(" ").append(i.fHelp).append("\n");
-                os.append("# TYPE ").append(i.fName).append(" ").append(i.fType.fName).append("\n");
-                os.append(i.fName).append(" ").append(i.toString()).append("\n");
+            public void Run(CollectorType.CollectorValue i) {
+                os.append("# HELP ").append(i.GetType().fName).append(" ").append(i.GetType().fHelp).append("\n");
+                os.append("# TYPE ").append(i.GetType().fName).append(" ").append(i.GetType().fType).append("\n");
+                os.append(i.GetKey()).append(" ").append(i.toString()).append("\n");
             }
         });
         SendResponse(httpExchange, 200, "text/plain", os.toString());

+ 1 - 1
app/src/main/java/info/knacki/prometheusandroidexporter/ICollector.java

@@ -5,5 +5,5 @@ import android.content.Context;
 import java.util.Collection;
 
 public interface ICollector {
-    Collection<CollectorValue> ReadValues(Context ctx);
+    Collection<CollectorType.CollectorValue> ReadValues(Context ctx);
 }

+ 2 - 0
app/src/main/java/info/knacki/prometheusandroidexporter/MainService.java

@@ -18,6 +18,7 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import info.knacki.prometheusandroidexporter.collector.DeviceCollector;
+import info.knacki.prometheusandroidexporter.collector.MemoryCollector;
 import info.knacki.prometheusandroidexporter.collector.PowerCollector;
 
 public class MainService extends Service {
@@ -64,6 +65,7 @@ public class MainService extends Service {
         //manager.RegisterCollector(new TestCollector());
         manager.RegisterCollector(new DeviceCollector());
         manager.RegisterCollector(new PowerCollector());
+        manager.RegisterCollector(new MemoryCollector());
     }
 
     private void ScheduleCollection() {

+ 8 - 7
app/src/main/java/info/knacki/prometheusandroidexporter/collector/DeviceCollector.java

@@ -6,21 +6,22 @@ import android.os.SystemClock;
 import java.util.ArrayDeque;
 import java.util.Collection;
 
-import info.knacki.prometheusandroidexporter.CollectorValue;
+import info.knacki.prometheusandroidexporter.CollectorType;
+import info.knacki.prometheusandroidexporter.CollectorType.CollectorValue;
 import info.knacki.prometheusandroidexporter.ICollector;
 
 public class DeviceCollector implements ICollector {
-    private final CollectorValue deviceUptimeCollector = new CollectorValue("device_uptime", "Device uptime (hours)", CollectorValue.Type.COUNTER);
-    private final double androidStartHours;
+    private final CollectorType deviceUptimeCollector = new CollectorType("device_uptime_sec", "Device uptime", CollectorType.Type.COUNTER);
+    private final double fAndroidBootTime;
 
     public DeviceCollector() {
-        androidStartHours = (System.currentTimeMillis() - SystemClock.uptimeMillis()) / (1000 * 60 * 60);
+        fAndroidBootTime = (System.currentTimeMillis() - SystemClock.uptimeMillis());
     }
 
     private CollectorValue Uptime() {
-        CollectorValue val = new CollectorValue(deviceUptimeCollector);
-        double currentHours = System.currentTimeMillis() / (1000. * 60 * 60);
-        val.SetValue((int) Math.round(currentHours - androidStartHours));
+        CollectorValue val = deviceUptimeCollector.new CollectorValue();
+        double uptime = System.currentTimeMillis() - fAndroidBootTime;
+        val.SetValue((int) Math.round(uptime / 1000.));
         return val;
     }
 

+ 31 - 0
app/src/main/java/info/knacki/prometheusandroidexporter/collector/MemoryCollector.java

@@ -0,0 +1,31 @@
+package info.knacki.prometheusandroidexporter.collector;
+
+import android.app.ActivityManager;
+import android.content.Context;
+
+import java.util.ArrayDeque;
+import java.util.Collection;
+
+import info.knacki.prometheusandroidexporter.CollectorType;
+import info.knacki.prometheusandroidexporter.ICollector;
+
+public class MemoryCollector implements ICollector {
+    private final CollectorType usedMemoryCollector = new CollectorType("memory_used_bytes", "Used memory", CollectorType.Type.GAUGE);
+    private final CollectorType freeMemoryCollector = new CollectorType("memory_free_bytes", "Available memory", CollectorType.Type.GAUGE);
+    private final CollectorType totalMemoryCollector = new CollectorType("memory_total_bytes", "Total memory", CollectorType.Type.GAUGE);
+
+    @Override
+    public Collection<CollectorType.CollectorValue> ReadValues(Context ctx) {
+        ArrayDeque<CollectorType.CollectorValue> result = new ArrayDeque<>();
+        ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
+        ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
+        if (manager != null) {
+            manager.getMemoryInfo(memInfo);
+
+            result.push(usedMemoryCollector.new CollectorValue().SetValue(memInfo.totalMem - memInfo.availMem));
+            result.push(freeMemoryCollector.new CollectorValue().SetValue(memInfo.availMem));
+            result.push(totalMemoryCollector.new CollectorValue().SetValue(memInfo.totalMem));
+        }
+        return result;
+    }
+}

+ 17 - 0
app/src/main/java/info/knacki/prometheusandroidexporter/collector/NetworkCollector.java

@@ -0,0 +1,17 @@
+package info.knacki.prometheusandroidexporter.collector;
+
+import android.content.Context;
+
+import java.util.ArrayDeque;
+import java.util.Collection;
+
+import info.knacki.prometheusandroidexporter.CollectorType;
+import info.knacki.prometheusandroidexporter.ICollector;
+
+public class NetworkCollector implements ICollector {
+    @Override
+    public Collection<CollectorType.CollectorValue> ReadValues(Context ctx) {
+        // FIXME
+        return new ArrayDeque<>();
+    }
+}

+ 11 - 32
app/src/main/java/info/knacki/prometheusandroidexporter/collector/PowerCollector.java

@@ -1,6 +1,5 @@
 package info.knacki.prometheusandroidexporter.collector;
 
-import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -9,16 +8,14 @@ import android.os.BatteryManager;
 import java.util.ArrayDeque;
 import java.util.Collection;
 
-import info.knacki.prometheusandroidexporter.CollectorValue;
+import info.knacki.prometheusandroidexporter.CollectorType;
+import info.knacki.prometheusandroidexporter.CollectorType.CollectorValue;
 import info.knacki.prometheusandroidexporter.ICollector;
 
 public class PowerCollector implements ICollector {
-    private final CollectorValue batteryLevelCollector = new CollectorValue("battery_level", "Battery level", CollectorValue.Type.COUNTER);
-    private final CollectorValue batteryStatusCollector = new CollectorValue("battery_status", "Battery level", CollectorValue.Type.STRING);
-    private final CollectorValue batteryPluggedCollector = new CollectorValue("battery_plugged", "Battery plugged", CollectorValue.Type.STRING);
-    private final CollectorValue batteryHealthCollector = new CollectorValue("battery_health", "Battery health", CollectorValue.Type.STRING);
-    private final CollectorValue batteryTemperatureCollector = new CollectorValue("battery_temp", "Battery temperature", CollectorValue.Type.COUNTER);
-    private final CollectorValue batteryVoltageCollector = new CollectorValue("battery_voltage", "Battery voltage (mV)", CollectorValue.Type.COUNTER);
+    private final CollectorType batteryLevelCollector = new CollectorType("battery_level", "Battery level", CollectorType.Type.GAUGE);
+    private final CollectorType batteryTemperatureCollector = new CollectorType("battery_temp", "Battery temperature", CollectorType.Type.GAUGE);
+    private final CollectorType batteryVoltageCollector = new CollectorType("battery_voltage", "Battery voltage (mV)", CollectorType.Type.GAUGE);
 
     private double GetBatteryLevel(Intent batteryStatus) {
         double batteryMax = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, 0) : 0;
@@ -77,31 +74,16 @@ public class PowerCollector implements ICollector {
     }
 
     private CollectorValue BatteryLevel(Intent batteryStatus) {
-        CollectorValue val = new CollectorValue(batteryLevelCollector);
+        CollectorValue val = batteryLevelCollector.new CollectorValue();
         val.SetValue(GetBatteryLevel(batteryStatus));
-        return val;
-    }
-
-    private CollectorValue BatteryStatus(Intent batteryStatus) {
-        CollectorValue val = new CollectorValue(batteryStatusCollector);
-        val.SetValue(GetBatteryStatus(batteryStatus));
-        return val;
-    }
-
-    private CollectorValue BatteryPlugged(Intent batteryStatus) {
-        CollectorValue val = new CollectorValue(batteryPluggedCollector);
-        val.SetValue(GetBatteryPlugged(batteryStatus));
-        return val;
-    }
-
-    private CollectorValue BatteryHealth(Intent batteryStatus) {
-        CollectorValue val = new CollectorValue(batteryHealthCollector);
-        val.SetValue(GetBatteryHealth(batteryStatus));
+        val.AddParameter("Status", GetBatteryStatus(batteryStatus));
+        val.AddParameter("Plugged", GetBatteryPlugged(batteryStatus));
+        val.AddParameter("Health", GetBatteryHealth(batteryStatus));
         return val;
     }
 
     private CollectorValue BatteryTemperature(Intent battery) {
-        CollectorValue val = new CollectorValue(batteryTemperatureCollector);
+        CollectorValue val = batteryTemperatureCollector.new CollectorValue();
         final int error = -100000;
         int value = battery == null ? error : battery.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, error);
         val.SetValue(value == error ? Double.NaN : ((double) value) / 10);
@@ -109,7 +91,7 @@ public class PowerCollector implements ICollector {
     }
 
     private CollectorValue BatteryVoltage(Intent battery) {
-        CollectorValue val = new CollectorValue(batteryVoltageCollector);
+        CollectorValue val = batteryVoltageCollector.new CollectorValue();
         final int error = -100000;
         int value = battery == null ? error : battery.getIntExtra(BatteryManager.EXTRA_VOLTAGE, error);
         val.SetValue(value == error ? Double.NaN : ((double) value) / 10);
@@ -121,9 +103,6 @@ public class PowerCollector implements ICollector {
         Intent batteryStatus = ctx.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
         ArrayDeque<CollectorValue> result = new ArrayDeque<>();
         result.add(BatteryLevel(batteryStatus));
-        result.add(BatteryStatus(batteryStatus));
-        result.add(BatteryPlugged(batteryStatus));
-        result.add(BatteryHealth(batteryStatus));
         result.add(BatteryTemperature(batteryStatus));
         result.add(BatteryVoltage(batteryStatus));
         // TODO BatteryManager.computeChargeTimeRemaining Time before battery get fully charged, msec, API >= 28

+ 17 - 0
app/src/main/java/info/knacki/prometheusandroidexporter/collector/StorageCollector.java

@@ -0,0 +1,17 @@
+package info.knacki.prometheusandroidexporter.collector;
+
+import android.content.Context;
+
+import java.util.ArrayDeque;
+import java.util.Collection;
+
+import info.knacki.prometheusandroidexporter.CollectorType;
+import info.knacki.prometheusandroidexporter.ICollector;
+
+public class StorageCollector implements ICollector {
+    @Override
+    public Collection<CollectorType.CollectorValue> ReadValues(Context ctx) {
+        // FIXME
+        return new ArrayDeque<>();
+    }
+}

+ 10 - 11
app/src/main/java/info/knacki/prometheusandroidexporter/collector/TestCollector.java

@@ -6,36 +6,35 @@ import java.util.ArrayDeque;
 import java.util.Calendar;
 import java.util.Collection;
 
-import info.knacki.prometheusandroidexporter.CollectorValue;
+import info.knacki.prometheusandroidexporter.CollectorType;
+import info.knacki.prometheusandroidexporter.CollectorType.CollectorValue;
 import info.knacki.prometheusandroidexporter.ICollector;
 
 public class TestCollector implements ICollector {
-    private final CollectorValue countCollector = new CollectorValue("test_counter", "A count that counts", CollectorValue.Type.COUNTER);
-    private final CollectorValue timerCollector = new CollectorValue("test_timer", "A clock that counts", CollectorValue.Type.COUNTER);
-    private final CollectorValue startedAtCollector = new CollectorValue("test_started", "A clock that counts", CollectorValue.Type.COUNTER);
+    private final CollectorType countCollector = new CollectorType("test_counter", "A count that counts", CollectorType.Type.COUNTER);
+    private final CollectorType timerCollector = new CollectorType("test_timer", "A clock that counts", CollectorType.Type.COUNTER);
+    private final CollectorType startedAtCollector = new CollectorType("test_started", "A clock that counts", CollectorType.Type.COUNTER);
     private final long serviceStart = System.currentTimeMillis();
     private int count =0;
 
     private CollectorValue ReadCounter() {
-        CollectorValue val = new CollectorValue(countCollector);
+        CollectorValue val = countCollector.new CollectorValue();
         val.SetValue(count++);
         return val;
     }
 
     private CollectorValue ReadTimer() {
-        CollectorValue val = new CollectorValue(timerCollector);
+        CollectorValue val = timerCollector.new CollectorValue();
         Calendar cal = Calendar.getInstance();
-        int minutes = cal.get(Calendar.MINUTE);
-        val.SetValue(cal.get(Calendar.HOUR_OF_DAY) +(minutes < 10 ? "0" : "") +minutes);
+        val.SetValue(cal.get(Calendar.HOUR_OF_DAY) * 100 + cal.get(Calendar.MINUTE));
         return val;
     }
 
     private CollectorValue ServiceStartedAt() {
-        CollectorValue val = new CollectorValue(startedAtCollector);
+        CollectorValue val = startedAtCollector.new CollectorValue();
         Calendar cal = Calendar.getInstance();
         cal.setTimeInMillis(serviceStart);
-        int minutes = cal.get(Calendar.MINUTE);
-        val.SetValue(cal.get(Calendar.HOUR_OF_DAY) +(minutes < 10 ? "0" : "") +minutes);
+        val.SetValue(cal.get(Calendar.HOUR_OF_DAY) * 100 + cal.get(Calendar.MINUTE));
         return val;
     }