Ver código fonte

net / storage data export

isundil 4 anos atrás
pai
commit
52691fab03

+ 3 - 2
app/src/main/java/info/knacki/prometheusandroidexporter/CollectorType.java

@@ -32,7 +32,7 @@ public class CollectorType {
 
     public class CollectorValue {
         private String valueAsString;
-        private Map<String, String> fParameters = new HashMap<>();
+        private final Map<String, String> fParameters = new HashMap<>();
 
         public String GetKey() {
             StringBuilder result = new StringBuilder(fName);
@@ -62,8 +62,9 @@ public class CollectorType {
             return this;
         }
 
-        public void AddParameter(String key, String val) {
+        public CollectorValue AddParameter(String key, String val) {
             fParameters.put(key, val);
+            return this;
         }
 
         @NonNull

+ 2 - 4
app/src/main/java/info/knacki/prometheusandroidexporter/CollectorWorker.java

@@ -8,10 +8,8 @@ import androidx.work.WorkerParameters;
 
 import com.google.common.util.concurrent.ListenableFuture;
 
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executor;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
 
 public class CollectorWorker extends ListenableWorker {
     /**
@@ -48,12 +46,12 @@ public class CollectorWorker extends ListenableWorker {
             }
 
             @Override
-            public Result get() throws ExecutionException, InterruptedException {
+            public Result get() {
                 return Result.success();
             }
 
             @Override
-            public Result get(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException {
+            public Result get(long timeout, @NonNull TimeUnit unit) {
                 return Result.success();
             }
         };

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

@@ -10,6 +10,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.net.InetSocketAddress;
 import java.nio.charset.Charset;
+import java.util.HashSet;
 import java.util.concurrent.Executors;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -82,11 +83,16 @@ public class HttpService {
         if (FilterRequests(httpExchange))
             return;
         final StringBuilder os = new StringBuilder();
+        final HashSet<String> servedVariables = new HashSet<>();
+
         CollectorManager.GetInstance().forEach(new CollectorManager.Runnable() {
             @Override
             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");
+                if (!servedVariables.contains(i.GetType().fName)) {
+                    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");
+                    servedVariables.add(i.GetType().fName);
+                }
                 os.append(i.GetKey()).append(" ").append(i.toString()).append("\n");
             }
         });

+ 2 - 8
app/src/main/java/info/knacki/prometheusandroidexporter/MainActivity.java

@@ -1,22 +1,16 @@
 package info.knacki.prometheusandroidexporter;
 
-import androidx.appcompat.app.AppCompatActivity;
-
 import android.app.ActivityManager;
 import android.content.Context;
-import android.content.Intent;
 import android.os.Bundle;
-import android.provider.SyncStateContract;
 import android.view.View;
-import android.widget.QuickContactBadge;
 import android.widget.TextView;
 
+import androidx.appcompat.app.AppCompatActivity;
+
 import java.util.List;
-import java.util.logging.Logger;
 
 public class MainActivity extends AppCompatActivity {
-    private final static Logger log = Logger.getLogger(MainActivity.class.getName());
-
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

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

@@ -6,6 +6,7 @@ import android.app.PendingIntent;
 import android.app.Service;
 import android.content.Context;
 import android.content.Intent;
+import android.os.Build;
 import android.os.IBinder;
 
 import androidx.annotation.Nullable;
@@ -19,7 +20,9 @@ import java.util.logging.Logger;
 
 import info.knacki.prometheusandroidexporter.collector.DeviceCollector;
 import info.knacki.prometheusandroidexporter.collector.MemoryCollector;
+import info.knacki.prometheusandroidexporter.collector.NetworkCollector;
 import info.knacki.prometheusandroidexporter.collector.PowerCollector;
+import info.knacki.prometheusandroidexporter.collector.StorageCollector;
 
 public class MainService extends Service {
     public MainService() {
@@ -66,6 +69,9 @@ public class MainService extends Service {
         manager.RegisterCollector(new DeviceCollector());
         manager.RegisterCollector(new PowerCollector());
         manager.RegisterCollector(new MemoryCollector());
+        manager.RegisterCollector(new NetworkCollector());
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
+            manager.RegisterCollector(new StorageCollector());
     }
 
     private void ScheduleCollection() {

+ 19 - 2
app/src/main/java/info/knacki/prometheusandroidexporter/collector/NetworkCollector.java

@@ -1,6 +1,7 @@
 package info.knacki.prometheusandroidexporter.collector;
 
 import android.content.Context;
+import android.net.TrafficStats;
 
 import java.util.ArrayDeque;
 import java.util.Collection;
@@ -9,9 +10,25 @@ import info.knacki.prometheusandroidexporter.CollectorType;
 import info.knacki.prometheusandroidexporter.ICollector;
 
 public class NetworkCollector implements ICollector {
+    private final CollectorType netRxBytes = new CollectorType("net_received", "received bytes", CollectorType.Type.COUNTER);
+    private final CollectorType netTxBytes = new CollectorType("net_transmit", "sent bytes", CollectorType.Type.COUNTER);
+
     @Override
     public Collection<CollectorType.CollectorValue> ReadValues(Context ctx) {
-        // FIXME
-        return new ArrayDeque<>();
+        final Collection<CollectorType.CollectorValue> result = new ArrayDeque<>();
+
+        result.add(netRxBytes.new CollectorValue()
+                .SetValue(TrafficStats.getMobileRxBytes())
+                .AddParameter("sources", "mobile"));
+        result.add(netRxBytes.new CollectorValue()
+                .SetValue(TrafficStats.getTotalRxBytes())
+                .AddParameter("sources", "total"));
+        result.add(netTxBytes.new CollectorValue()
+                .SetValue(TrafficStats.getMobileTxBytes())
+                .AddParameter("sources", "mobile"));
+        result.add(netTxBytes.new CollectorValue()
+                .SetValue(TrafficStats.getTotalTxBytes())
+                .AddParameter("sources", "total"));
+        return result;
     }
 }

+ 12 - 14
app/src/main/java/info/knacki/prometheusandroidexporter/collector/PowerCollector.java

@@ -16,6 +16,7 @@ public class PowerCollector implements ICollector {
     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 final CollectorType batteryStatusCollector = new CollectorType("battery_status", "Battery status", CollectorType.Type.GAUGE);
 
     private double GetBatteryLevel(Intent batteryStatus) {
         double batteryMax = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, 0) : 0;
@@ -73,38 +74,35 @@ public class PowerCollector implements ICollector {
         return "Unknown";
     }
 
-    private CollectorValue BatteryLevel(Intent batteryStatus) {
-        CollectorValue val = batteryLevelCollector.new CollectorValue();
-        val.SetValue(GetBatteryLevel(batteryStatus));
+    private CollectorValue BatteryStatus(Intent batteryStatus) {
+        CollectorValue val = batteryStatusCollector.new CollectorValue();
+        val.SetValue(1);
         val.AddParameter("Status", GetBatteryStatus(batteryStatus));
         val.AddParameter("Plugged", GetBatteryPlugged(batteryStatus));
         val.AddParameter("Health", GetBatteryHealth(batteryStatus));
         return val;
     }
 
-    private CollectorValue BatteryTemperature(Intent battery) {
-        CollectorValue val = batteryTemperatureCollector.new CollectorValue();
+    private double GetBatteryTemperature(Intent battery) {
         final int error = -100000;
         int value = battery == null ? error : battery.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, error);
-        val.SetValue(value == error ? Double.NaN : ((double) value) / 10);
-        return val;
+        return value == error ? error : ((double) value) / 10.;
     }
 
-    private CollectorValue BatteryVoltage(Intent battery) {
-        CollectorValue val = batteryVoltageCollector.new CollectorValue();
+    private double GetBatteryVoltage(Intent battery) {
         final int error = -100000;
         int value = battery == null ? error : battery.getIntExtra(BatteryManager.EXTRA_VOLTAGE, error);
-        val.SetValue(value == error ? Double.NaN : ((double) value) / 10);
-        return val;
+        return value == error ? Double.NaN : ((double) value) / 10.;
     }
 
     @Override
     public Collection<CollectorValue> ReadValues(Context ctx) {
         Intent batteryStatus = ctx.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
         ArrayDeque<CollectorValue> result = new ArrayDeque<>();
-        result.add(BatteryLevel(batteryStatus));
-        result.add(BatteryTemperature(batteryStatus));
-        result.add(BatteryVoltage(batteryStatus));
+        result.add(BatteryStatus(batteryStatus));
+        result.add(batteryLevelCollector.new CollectorValue().SetValue(GetBatteryLevel(batteryStatus)));
+        result.add(batteryTemperatureCollector.new CollectorValue().SetValue(GetBatteryTemperature(batteryStatus)));
+        result.add(batteryVoltageCollector.new CollectorValue().SetValue(GetBatteryVoltage(batteryStatus)));
         // TODO BatteryManager.computeChargeTimeRemaining Time before battery get fully charged, msec, API >= 28
         return result;
     }

+ 32 - 2
app/src/main/java/info/knacki/prometheusandroidexporter/collector/StorageCollector.java

@@ -1,6 +1,11 @@
 package info.knacki.prometheusandroidexporter.collector;
 
 import android.content.Context;
+import android.os.Build;
+import android.os.Environment;
+import android.os.StatFs;
+
+import androidx.annotation.RequiresApi;
 
 import java.util.ArrayDeque;
 import java.util.Collection;
@@ -8,10 +13,35 @@ import java.util.Collection;
 import info.knacki.prometheusandroidexporter.CollectorType;
 import info.knacki.prometheusandroidexporter.ICollector;
 
+@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
 public class StorageCollector implements ICollector {
+    private static final CollectorType externalStorageAvailable = new CollectorType("fs_external_available_bool", "boolean value indicating if external storage is available", CollectorType.Type.GAUGE);
+    private static final CollectorType availableStorage = new CollectorType("fs_available_storage_bytes", "available storage", CollectorType.Type.GAUGE);
+    private static final CollectorType usedStorage = new CollectorType("fs_total_storage_bytes", "total storage", CollectorType.Type.GAUGE);
+
+    private boolean ExternalStorageAvailable() {
+        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) || Environment.isExternalStorageEmulated();
+    }
+
+    private StatFs GetExtrernalStorageStats() {
+        return new StatFs(Environment.getExternalStorageDirectory().getPath());
+    }
+
+    private StatFs GetInternalStorageStats() {
+        return new StatFs(Environment.getDataDirectory().getPath());
+    }
+
     @Override
     public Collection<CollectorType.CollectorValue> ReadValues(Context ctx) {
-        // FIXME
-        return new ArrayDeque<>();
+        final Collection<CollectorType.CollectorValue> result = new ArrayDeque<>();
+        final StatFs externalStats = GetExtrernalStorageStats();
+        final StatFs internalStats = GetInternalStorageStats();
+
+        result.add(externalStorageAvailable.new CollectorValue().SetValue(ExternalStorageAvailable() ? 1 : 0));
+        result.add(availableStorage.new CollectorValue().SetValue(externalStats.getTotalBytes()).AddParameter("source", "external"));
+        result.add(availableStorage.new CollectorValue().SetValue(internalStats.getTotalBytes()).AddParameter("source", "internal"));
+        result.add(usedStorage.new CollectorValue().SetValue(externalStats.getAvailableBytes()).AddParameter("source", "external"));
+        result.add(usedStorage.new CollectorValue().SetValue(internalStats.getAvailableBytes()).AddParameter("source", "internal"));
+        return result;
     }
 }

+ 0 - 30
app/src/main/res/drawable-v24/ic_launcher_foreground.xml

@@ -1,30 +0,0 @@
-<vector xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:aapt="http://schemas.android.com/aapt"
-    android:width="108dp"
-    android:height="108dp"
-    android:viewportWidth="108"
-    android:viewportHeight="108">
-    <path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
-        <aapt:attr name="android:fillColor">
-            <gradient
-                android:endX="85.84757"
-                android:endY="92.4963"
-                android:startX="42.9492"
-                android:startY="49.59793"
-                android:type="linear">
-                <item
-                    android:color="#44000000"
-                    android:offset="0.0" />
-                <item
-                    android:color="#00000000"
-                    android:offset="1.0" />
-            </gradient>
-        </aapt:attr>
-    </path>
-    <path
-        android:fillColor="#FFFFFF"
-        android:fillType="nonZero"
-        android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
-        android:strokeWidth="1"
-        android:strokeColor="#00000000" />
-</vector>

+ 0 - 170
app/src/main/res/drawable/ic_launcher_background.xml

@@ -1,170 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<vector xmlns:android="http://schemas.android.com/apk/res/android"
-    android:width="108dp"
-    android:height="108dp"
-    android:viewportWidth="108"
-    android:viewportHeight="108">
-    <path
-        android:fillColor="#3DDC84"
-        android:pathData="M0,0h108v108h-108z" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M9,0L9,108"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M19,0L19,108"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M29,0L29,108"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M39,0L39,108"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M49,0L49,108"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M59,0L59,108"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M69,0L69,108"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M79,0L79,108"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M89,0L89,108"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M99,0L99,108"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M0,9L108,9"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M0,19L108,19"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M0,29L108,29"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M0,39L108,39"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M0,49L108,49"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M0,59L108,59"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M0,69L108,69"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M0,79L108,79"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M0,89L108,89"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M0,99L108,99"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M19,29L89,29"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M19,39L89,39"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M19,49L89,49"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M19,59L89,59"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M19,69L89,69"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M19,79L89,79"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M29,19L29,89"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M39,19L39,89"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M49,19L49,89"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M59,19L59,89"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M69,19L69,89"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-    <path
-        android:fillColor="#00000000"
-        android:pathData="M79,19L79,89"
-        android:strokeWidth="0.8"
-        android:strokeColor="#33FFFFFF" />
-</vector>

+ 0 - 2
app/src/main/res/layout/activity_main.xml

@@ -1,6 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
@@ -19,7 +18,6 @@
             android:orientation="horizontal">
 
             <TextView
-                android:id="@+id/textView"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"

+ 0 - 4
app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml

@@ -1,4 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
-    <foreground android:drawable="@mipmap/ic_launcher_foreground"/>
-</adaptive-icon>

BIN
app/src/main/res/mipmap-hdpi/ic_launcher_round.png


BIN
app/src/main/res/mipmap-mdpi/ic_launcher_round.png


BIN
app/src/main/res/mipmap-xhdpi/ic_launcher_round.png


BIN
app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png


BIN
app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png