Skip to content

Instantly share code, notes, and snippets.

@rawnly
Last active July 25, 2023 09:01
Show Gist options
  • Save rawnly/76935c517c836110149b1cd638e77eb0 to your computer and use it in GitHub Desktop.
Save rawnly/76935c517c836110149b1cd638e77eb0 to your computer and use it in GitHub Desktop.
#! /bin/bash
# date (YYYY-MM-DD HH:MM:SS),percentage
datetime="$(date +%Y-%m-%d\ %H:%M:%S)"
memory_usage="$(ps -A -o %mem | awk '{ mem += $1 } END { print mem }')"
uptime="$(uptime | awk '{ print $3 }' | cut -d, -f1)"
# number only
battery_percentage="$(pmset -g batt | grep -Eo "\d+%" | cut -d% -f1)"
# append to file
FILENAME="$HOME/path-to-your-project/data/battery.csv"
# if file doesn't exist, create it and add header
if [ ! -f $FILENAME ]; then
echo "datetime,percentage,memory_usage,uptime" >>$FILENAME
fi
# append data to file
echo "${datetime},${battery_percentage},${memory_usage},${uptime}" >>$FILENAME
*/1 * * * * ~/Developer/crons/battery-collector.sh
{
"__inputs": [
{
"name": "DS_CSV",
"label": "CSV",
"description": "",
"type": "datasource",
"pluginId": "marcusolsson-csv-datasource",
"pluginName": "CSV"
}
],
"__elements": {},
"__requires": [
{
"type": "panel",
"id": "gauge",
"name": "Gauge",
"version": ""
},
{
"type": "grafana",
"id": "grafana",
"name": "Grafana",
"version": "10.0.2"
},
{
"type": "datasource",
"id": "marcusolsson-csv-datasource",
"name": "CSV",
"version": "0.6.5"
},
{
"type": "panel",
"id": "timeseries",
"name": "Time series",
"version": ""
}
],
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": {
"type": "grafana",
"uid": "-- Grafana --"
},
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 1,
"id": null,
"links": [],
"liveNow": true,
"panels": [
{
"datasource": {
"type": "marcusolsson-csv-datasource",
"uid": "${DS_CSV}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "continuous-BlPu"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisGridShow": true,
"axisLabel": "Value",
"axisPlacement": "auto",
"axisSoftMax": 100,
"axisSoftMin": 0,
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 2,
"gradientMode": "opacity",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "stepBefore",
"lineStyle": {
"fill": "solid"
},
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "never",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"max": 100,
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "percent"
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 0,
"y": 0
},
"id": 4,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"timezone": [
"browser"
],
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "marcusolsson-csv-datasource",
"uid": "${DS_CSV}"
},
"decimalSeparator": ".",
"delimiter": ",",
"header": true,
"ignoreUnknown": false,
"path": "battery.csv",
"refId": "A",
"schema": [
{
"name": "percentage",
"type": "number"
},
{
"name": "memory_usage",
"type": "number"
},
{
"name": "datetime",
"type": "time"
}
],
"skipRows": 0,
"timezone": "Europe/Rome"
}
],
"title": "Panel Title",
"type": "timeseries"
},
{
"datasource": {
"type": "marcusolsson-csv-datasource",
"uid": "${DS_CSV}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisGridShow": true,
"axisLabel": "",
"axisPlacement": "auto",
"axisSoftMax": 100,
"axisSoftMin": 0,
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 23,
"gradientMode": "opacity",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "never",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"decimals": 0,
"mappings": [],
"max": 100,
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "red",
"value": null
},
{
"color": "orange",
"value": 25
},
{
"color": "yellow",
"value": 50
},
{
"color": "green",
"value": 80
}
]
},
"unit": "percent"
},
"overrides": []
},
"gridPos": {
"h": 10,
"w": 12,
"x": 12,
"y": 0
},
"id": 1,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": false
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "marcusolsson-csv-datasource",
"uid": "${DS_CSV}"
},
"decimalSeparator": ".",
"delimiter": ",",
"header": true,
"ignoreUnknown": false,
"path": "battery.csv",
"refId": "A",
"schema": [
{
"name": "percentage",
"type": "number"
},
{
"name": "datetime",
"type": "time"
}
],
"skipRows": 0,
"timezone": "Europe/Rome"
}
],
"title": "Battery Consumption",
"type": "timeseries"
},
{
"datasource": {
"type": "marcusolsson-csv-datasource",
"uid": "${DS_CSV}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "continuous-BlPu"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"axisSoftMax": 100,
"axisSoftMin": 0,
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 3,
"gradientMode": "hue",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineStyle": {
"dash": [
10,
10
],
"fill": "dash"
},
"lineWidth": 1,
"pointSize": 8,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "dashed"
}
},
"displayName": "Memory",
"mappings": [],
"max": 100,
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "percent"
},
"overrides": []
},
"gridPos": {
"h": 10,
"w": 12,
"x": 0,
"y": 8
},
"id": 3,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": false
},
"tooltip": {
"mode": "multi",
"sort": "none"
}
},
"pluginVersion": "10.0.2",
"targets": [
{
"datasource": {
"type": "marcusolsson-csv-datasource",
"uid": "${DS_CSV}"
},
"decimalSeparator": ".",
"delimiter": ",",
"header": true,
"ignoreUnknown": false,
"path": "battery.csv",
"refId": "A",
"schema": [
{
"name": "datetime",
"type": "time"
},
{
"name": "memory_usage",
"type": "number"
}
],
"skipRows": 0,
"timezone": "Europe/Rome"
}
],
"title": "Memory",
"type": "timeseries"
},
{
"datasource": {
"type": "marcusolsson-csv-datasource",
"uid": "${DS_CSV}"
},
"description": "",
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"custom": {
"neutral": 2
},
"mappings": [
{
"options": {
"100": {
"index": 0,
"text": "Fully Charged"
}
},
"type": "value"
}
],
"thresholds": {
"mode": "percentage",
"steps": [
{
"color": "red",
"value": null
},
{
"color": "orange",
"value": 25
},
{
"color": "yellow",
"value": 50
},
{
"color": "green",
"value": 79.9995
}
]
},
"unit": "percent"
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 12,
"x": 12,
"y": 10
},
"id": 2,
"options": {
"orientation": "auto",
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "/^percentage$/",
"values": false
},
"showThresholdLabels": false,
"showThresholdMarkers": false
},
"pluginVersion": "10.0.2",
"targets": [
{
"datasource": {
"type": "marcusolsson-csv-datasource",
"uid": "${DS_CSV}"
},
"decimalSeparator": ".",
"delimiter": ",",
"header": true,
"ignoreUnknown": false,
"path": "battery.csv",
"refId": "A",
"schema": [
{
"name": "percentage",
"type": "number"
}
],
"skipRows": 0
}
],
"title": "Charge",
"type": "gauge"
}
],
"refresh": false,
"schemaVersion": 38,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "2023-07-24T20:32:00.000Z",
"to": "2023-07-25T08:02:00.000Z"
},
"timepicker": {
"hidden": false,
"refresh_intervals": [
"5s",
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d"
],
"time_options": [
"5m",
"15m",
"1h",
"6h",
"12h",
"24h",
"2d",
"7d",
"30d"
],
"type": "timepicker"
},
"timezone": "browser",
"title": "Mac Status",
"uid": "dda0a184-03e7-4eaa-8595-92b6f215b214",
"version": 9,
"weekStart": "monday"
}
version: "3.8"
volumes:
grafana:
name: grafana-volume
services:
grafana:
image: grafana/grafana:latest
user: root
container_name: grafana
restart: unless-stopped
volumes:
- ./data:/data
- grafana:/var/lib/grafana
- ./grafana.ini:/etc/grafana/grafana.ini
ports:
- "5687:3000"
environment:
GF_SECURITY_ADMIN_USER: admin
GF_SECURITY_ADMIN_PASSWORD: admin
GF_INSTALL_PLUGINS: marcusolsson-csv-datasource
GF_PLUGIN_ALLOW_LOCAL_MODE: true
intance_name = ${HOSTNAME}
[security]
admin_user = admin
[plugin.marcusolsson-csv-datasource]
allow_local_mode = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment