Skip to content
Snippets Groups Projects
Commit f53af04e authored by Bjarne Janesch's avatar Bjarne Janesch
Browse files

deliverables for 'investigate system status'

parent 02e93ec3
No related branches found
No related tags found
No related merge requests found
Pipeline #67791 passed
#!/usr/bin/env bash
TMP_METRICS_WEB_DIR=$(mktemp -d)
LISTENING_PORT=3000
function getTotalInboundTraffic(){
local receivedBytes=0
local networkDevices=$(ls /sys/class/net/ | grep -v lo)
local bytesBefore=0
for device in ${networkDevices}; do
local bytesOnDevice=$(cat /sys/class/net/${device}/statistics/rx_bytes)
bytesBefore=$((${bytesBefore} + ${bytesOnDevice}))
done
sleep 1
local bytesAfter=0
for device in ${networkDevices}; do
local bytesOnDevice=$(cat /sys/class/net/${device}/statistics/rx_bytes)
bytesAfter=$((${bytesAfter} + ${bytesOnDevice}))
done
receivedBytes=$((${bytesAfter} - ${bytesBefore}))
echo "${receivedBytes}"
}
function getTotalCountOfProcesses(){
local processCount=0
processCount=$(ps aux | wc -l)
echo "${processCount}"
}
function getTotalMemoryUsage(){
local memoryInKibibytes=0
memoryInKibibytes=$(free -k | grep Mem | awk -F " " '{ print $2}')
echo "${memoryInKibibytes}"
}
function getTotalUptime(){
local uptimeInSeconds=0
uptimeInSeconds=$(cat /proc/uptime | awk -F " " '{ print $1}')
echo ""${uptimeInSeconds}""
}
# Replace file content after every sleep cycle
( while true; do
printf "%s\n" \
"# HELP process_count_total Amount of running processes" \
"# TYPE process_count_total gauge" \
"process_count_total $(getTotalCountOfProcesses)" \
"" \
"# HELP memory_utilization_total_kib Amount of currently used memory" \
"# TYPE memory_utilization_total_kib gauge" \
"memory_utilization_total_kib $(getTotalMemoryUsage)" \
"" \
"# HELP uptime_total_seconds Amount of time the system is running already" \
"# TYPE uptime_total_seconds counter" \
"uptime_total_seconds $(getTotalUptime)" \
"" \
"# HELP traffic_inbound_total_bytes_per_second Amount of receiving traffic within a second across all network interfaces, except lo" \
"# TYPE traffic_inbound_total_bytes_per_second gauge" \
"traffic_inbound_total_bytes_per_second $(getTotalInboundTraffic)" \
> "${TMP_METRICS_WEB_DIR}/index.html"
sleep 3;
done ) & PIDS[1]=$!
# Start web server to serve metrics file
( python3 -m http.server \
--bind 0.0.0.0 \
--directory "${TMP_METRICS_WEB_DIR}" \
"${LISTENING_PORT}"
) & PIDS[2]=$!
function quit(){
for PID in ${PIDS[*]}; do kill -s SIGTERM ${PID}; done;
rm -rf "${TMP_METRICS_WEB_DIR}"
exit 0
}
trap quit SIGINT SIGTERM
wait
\ No newline at end of file
bj@Bjarnes-Laptop:~/DevOps/tutorials/webservice/investigate_system_status$ dir
exporter.sh
bj@Bjarnes-Laptop:~/DevOps/tutorials/webservice/investigate_system_status$ chmod +x exporter.sh
bj@Bjarnes-Laptop:~/DevOps/tutorials/webservice/investigate_system_status$ chmod +x /DevOps/tutorial_webservice/investigate_system_status/exporter.sh ^C
bj@Bjarnes-Laptop:~/DevOps/tutorials/webservice/investigate_system_status$ ./exporter.sh
Serving HTTP on 0.0.0.0 port 3000 (http://0.0.0.0:3000/) ...
127.0.0.1 - - [28/Jul/2024 18:46:28] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [28/Jul/2024 18:46:28] code 404, message File not found
127.0.0.1 - - [28/Jul/2024 18:46:28] "GET /favicon.ico HTTP/1.1" 404 -
^C
Keyboard interrupt received, exiting.
bj@Bjarnes-Laptop:~/DevOps/tutorials/webservice/investigate_system_status$
Browser (localhost:3000):
# HELP process_count_total Amount of running processes # TYPE process_count_total gauge process_count_total 100 # HELP memory_utilization_total_kib Amount of currently used memory # TYPE memory_utilization_total_kib gauge memory_utilization_total_kib 3941156 # HELP uptime_total_seconds Amount of time the system is running already # TYPE uptime_total_seconds counter uptime_total_seconds 4479.20 # HELP traffic_inbound_total_bytes_per_second Amount of receiving traffic within a second across all network interfaces, except lo # TYPE traffic_inbound_total_bytes_per_second gauge traffic_inbound_total_bytes_per_second 66
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment