Repository navigation
Expand file tree
/
Copy pathscript.sh
More file actions
executable file
·207 lines (164 loc) · 6.96 KB
/
Copy pathscript.sh
File metadata and controls
executable file
·207 lines (164 loc) · 6.96 KB
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
#!/bin/bash
### CONFIGURATION FILE (optional, can be done via "config.env") ###
if [ -f "config.env" ]; then
source config.env
echo "loaded configuration from config file."
else
echo "config file not found. using script defaults."
fi
### VALUES ###
# The default is your router's IP, but it can be changed (eg. 1.1.1.1 or google.com)
PING_IP="${PING_IP:-8.8.8.8}"
PING_WAIT_TIME="${PING_WAIT_TIME:-1}"
PING_COUNT="${PING_COUNT:-5}"
# Gets the current date and time (adds it to the JSON output.)
CURRENT_TIME=$(date +"%d/%m/%Y @ %H:%M:%S UTC")
# The path to the repo/directory that will be pushed to GitHub.
REPO_PATH="${REPO_PATH:-$HOME/your/repo/name}"
# Path as to where the converted JSON output will be saved.
JSON_PATH="${JSON_PATH:-$REPO_PATH/stats.json}"
# Path as to where the badge-friendly JSON output will be saved.
BADGES_JSON_PATH="${BADGES_JSON_PATH:-$REPO_PATH/badges_stats.json}"
# The commit message the script will send out.
COMMIT_MSG="[AUTOMATED] updated: $(date +"%Y/%m/%d @ %H:%M:%S")"
### DEBUG MESSAGES ###
UPTIME_GET_MSG="${UPTIME_GET_MSG:-getting uptime...}"
CPU_TEMP_GET_MSG="${CPU_TEMP_GET_MSG:-getting cpu temperature...}"
BATTERY_PERCENTAGE_GET_MSG="${BATTERY_PERCENTAGE_GET_MSG:-getting battery percentage...}"
BATTERY_TIME_LEFT_MSG="${BATTERY_TIME_LEFT_MSG:-getting battery time left...}"
ENERGY_RATE_GET_MSG="${ENERGY_RATE_GET_MSG:-getting energy-rate...}"
PING_GET_MSG="${PING_GET_MSG:-getting ping... [ip: $PING_IP, count = $PING_COUNT, wait-time = $PING_WAIT_TIME second(s)]}"
### STATS ###
# You can comment-out stats you don't want to display.
# CPU TEMPERATURE (default is thermal_zone0)
echo $CPU_TEMP_GET_MSG
CPU_TEMP=$(cat /sys/class/thermal/thermal_zone0/temp)
CPU_TEMP=$((CPU_TEMP / 1000))
# The energy-rate (wattage) of the server (WHEN RUNNING ON BATTERY).
echo $ENERGY_RATE_GET_MSG
ENERGY_RATE=$(upower -b | awk '/energy-rate/ {print $2}')
ENERGY_RATE="$ENERGY_RATE W"
# The battery's percentage.
echo $BATTERY_PERCENTAGE_GET_MSG
BATTERY_PERCENTAGE=$(upower -b | awk '/percentage/ {print $2}')
echo $BATTERY_TIME_LEFT_MSG
BATTERY_TIME_LEFT=$(upower -b | awk '/time to empty/ {print $4}')
# The total uptime (how long the server has been on) of the device.
echo $UPTIME_GET_MSG
UPTIME=$(uptime -p | cut -b 4-)
# The average router ping latency (in miliseconds).
echo $PING_GET_MSG
LATENCY=$(ping -4 -W "$PING_WAIT_TIME" -c "$PING_COUNT" "$PING_IP" 2>/dev/null | tail -1 | awk '{print $4}' | cut -d '/' -f 2)
### VERIFYING ###
# The array "stats" holds all the variable names.
STATS=(
"CPU_TEMP"
"ENERGY_RATE"
"BATTERY_PERCENTAGE"
"BATTERY_TIME_LEFT"
"UPTIME"
"LATENCY"
)
# This for loop iterates over the array "stats" and hence checks each variable if their respective values are non-empty, empty, or if the variable is not defined (commented out) respectively.
for VALUE in "${STATS[@]}"
do
# Checks if the value of the variable is non-empty / nil / undefined.
# If value doesn't exist.
if [[ ! -v "$VALUE" ]]; then
echo "- UNDEFINED: The variable $VALUE does not exist."
continue
fi
# If variable ENERGY_RATE is equal to zero (when server in on AC power, or when it is charging.)
if [ "$VALUE" = "ENERGY_RATE" ]; then
if [ "${!VALUE}" = 0 ]; then
echo "- ON AC: The variable $VALUE is zero as the device is currently plugged in."
ENERGY_RATE="[PLUGGED IN]"
fi
if [ $(upower -b | awk '/state/ {print $2}') = "charging" ]; then
ENERGY_RATE="$ENERGY_RATE [PLUGGED IN]"
fi
fi
if [ "$VALUE" = "LATENCY" ]; then
if [ -z "$VALUE" ]; then
LATENCY="offline (the specified ping-ip is not responding)."
else
LATENCY="$LATENCY ms"
fi
fi
# If variable BATTERY_TIME_LEFT is equal to zero (when it is plugged in.)
if [ "$VALUE" = "BATTERY_TIME_LEFT" ]; then
if [ -z "${!VALUE}" ]; then
echo "- ON AC: The variable $VALUE is nil as the device is currently plugged in."
BATTERY_TIME_LEFT="[PLUGGED IN]"
continue
else
BATTERY_TIME_LEFT="$BATTERY_TIME_LEFT hours"
fi
fi
# VERIFICATION (it iterates over the array "STATS".)
if [ -n "${!VALUE}" ]; then
echo "- got $VALUE!"
echo "${!VALUE}"
# If value is nil / empty.
else
echo "- NIL: The variable $VALUE has no value or has encountered an error."
fi
done
### JSON FORMAT ###
# Outputs the values of each stat into a JSON format.
JSON_STRING=$(cat <<EOF
{
"last_updated":"$CURRENT_TIME",
"cpu_temperature":"$CPU_TEMP°C",
"energy_rate":"$ENERGY_RATE",
"battery_percentage":"$BATTERY_PERCENTAGE",
"battery_time_left":"$BATTERY_TIME_LEFT",
"uptime":"$UPTIME",
"latency":"$LATENCY"
}
EOF
)
# Makes the file in the specified directory (also makes the directory if not already made.)
mkdir -p "$(dirname "$JSON_PATH")"
# Outputs the converted JSON output into the specified JSON path.
echo "$JSON_STRING" > "$JSON_PATH"
echo "$JSON_STRING" > "$BADGES_JSON_PATH"
# Converts the JSON output into text that is URL-friendly.
sed -i "s|%|%25|g" $BADGES_JSON_PATH
sed -i "s|, |%2C%20|g" $BADGES_JSON_PATH
sed -i "s| |%20|g" $BADGES_JSON_PATH
# Just echoes/outputs the specified JSON path.
echo "- saved to: $JSON_PATH"
cat $JSON_PATH
### GIT PUSH ###
cd "$REPO_PATH" || exit 1
# Outputs the contents of the template into the README (flushing out the old values and generating the template for Github Actions to fill with the new values.)
cat template.md > README.md
# Stash the unpushed changes, rebuild the repo from the remote repo, and readd the unpushed changes. (Avoids issues when dealing with multiple commits and unpushed changes.)
git stash --quiet || echo "- nothing to stash or stash failed."
git pull origin main --rebase --quiet
git stash pop --quiet
# Checks if there are any differences between the local repo/directory and the repo up on GitHub.
# The tag "--quiet" makes the whole "git diff" return only an exit code. 0 is there are no changes, and 1 if there are changes.
# The "!" before the command inverts the output exit code. (i.e. If there is a change in the file, it will output the exit code 1, which then gets inverted into a 0, running the commands in the below if statement.)
# In shell, and exit code "0" marks a "true" value while an exit code "1" marks a "false" value.a
if ! git diff --quiet stats.json; then
# Adds the given JSON output(s) into the commit.
git add stats.json
git add badges_stats.json
# Outputs the contents of the template into the README (flushing out the old values and generating the template for Github Actions to fill with the new values.)
cat template.md > README.md
# Adds the changed README into the commit.
git add README.md
# Adds the configured commit message (configured via setting the "COMMIT_MSG" variable.)
git commit -m "$COMMIT_MSG"
# Pushes the commit to GitHub from origin (local files) to main (main branch on GitHub) and specifies where it got pushed to.
if git push origin main 2>&1; then
echo "- pushed to configured github repo."
else
echo "- push failed. remote may have new commits."
fi
else
# If there are no differences between the local repo and the remote repo, no commit will be pushed.
echo "- no changes to commit."
fi