You are not logged in.
Pages: 1
Hello,
i made a small bash script which monitors my vms. maybe it's useful for you.
vms are expected to run arch and have pacman-contrib installed (this can be changed easily though)
if you place a health.sh script in /root the exit code is evaluated
conkyrc on host:
${execpi 10 ssh root@{HOSTNAME} -C cat /tmp/info}
/etc/systemd/system/sysinfo.service on vm
[Unit]
Description=conkysysinfo
[Service]
ExecStart=/root/info.sh
[Install]
WantedBy=multi-user.target
/root/info.sh on vm
#!/bin/bash
OK="\${color green}"
FAIL="\${color red}"
trap "kill 0" SIGINT
NAME=`cat /etc/hostname`
echo "HEALTH=" > /tmp/info_health
if [ -f /root/health.sh ]; then
(
while :
do
HEALTH=`/root/health.sh && echo \\\${color green} || echo \\\${color red}`
echo "HEALTH=\"\\$HEALTH\"" > /tmp/info_health
sleep 300
done
) &
fi
echo "UPDATES=\"0\"" > /tmp/info_updates
(
while :
do
UPDATES=`checkupdates | wc -l`
echo "UPDATES=\"$UPDATES\"" > /tmp/info_updates
sleep 3600
done
) &
MEMTOTAL="0B"
MEMUSED="0B"
SWAPTOTAL=0B""
SWAPUSED="0B"
echo "" > /tmp/info_mem
(
while :
do
printf "MEMTOTAL=\"`free -h | awk '(NR==2){print $2}'`\"\n\
MEMUSED=\"`free -h | awk '(NR==2){print $3}'`\"\n\
SWAPTOTAL=\"`free -h | awk '(NR==3){print $2}'`\"\n\
SWAPUSED=\"`free -h | awk '(NR==3){print $3}'`\"\n" > /tmp/info_mem
sleep 10
done
) &
CPUCOUNT="0"
CPUPERCENT="0"
echo "" > /tmp/info_cpu
(
while :
do
echo -e "CPUCOUNT=\"`cat /proc/cpuinfo | grep processor | wc -l`\"\nCPUPERCENT=\"`top -bn10 | awk 'FNR == 3 { print $2+$4; }'`\"\n" > /tmp/info_cpu
sleep 5
done
) &
HDDSIZE="0B"
HDDUSED="0B"
echo "" > /tmp/info_hdd
(
while :
do
echo -e "HDDSIZE=\"`df -h | grep vda3 | awk '{print $2}'`\"\nHDDUSED=\"`df -h | grep vda3 | awk '{print $3}'`\"\n" > /tmp/info_hdd
sleep 300
done
) &
(
while :
do
(
. /tmp/info_health
. /tmp/info_updates
. /tmp/info_mem
. /tmp/info_cpu
. /tmp/info_hdd
echo -n $HEALTH$NAME\$color
if [ "$UPDATES" != "0" ]; then
echo -n "\$alignr$OK$UPDATES Updates\$color "
fi
echo
echo "\$hr"
echo "RAM \${color grey}$MEMUSED / $MEMTOTAL\$color \${goto 275} SWAP \${color grey}$SWAPUSED / $SWAPTOTAL \$color"
echo "CPU \${color grey}$CPUPERCENT% / $CPUCOUNT Threads\$color \${goto 275} HDD \${color grey}$HDDUSED / ${HDDSIZE} \$color"
) > /tmp/info
sleep 1
done
) &
wait
result:
https://i.imgur.com/1hkScWV.png
moderator edit -- replaced oversized image with link.
Pasting pictures and code
Last edited by 2ManyDogs (2019-10-17 22:15:56)
Offline
thanks Flower. this looks like it might come in handy
Offline
You could do a lot to clean up the script on the server - as just one example, rather than calling free and awk several times each while also creating several additional subshells, just call each one once:
#replace these lines
printf "MEMTOTAL=\"`free -h | awk '(NR==2){print $2}'`\"\n\
MEMUSED=\"`free -h | awk '(NR==2){print $3}'`\"\n\
SWAPTOTAL=\"`free -h | awk '(NR==3){print $2}'`\"\n\
SWAPUSED=\"`free -h | awk '(NR==3){print $3}'`\"\n" > /tmp/info_mem
# with this instead
free -h | awk '
NR==2 { printf "MEMTOTAL=\"%s\"\nMEMUSED=\"%s\"\n", $2, $3; }
NR==3 { printf "SWAPTOTAL=\"%s\"\nSWAPUSED=\"%s\"\n", $2, $3; }
' >/tmp/info_mem
Also, your loop times are quite odd. Why is the outermost process looping every second to regenerate /tmp/info when all of the content used to create /tmp/info is regenerated at much longer intervals? Specifically, the shortest interval of any component is 5 seconds, yet the /tmp/info is regerated 5 times in that time: once with new data, then 4 times for no reason with stale data.
EDIT: that entire script can be replaced by the following:
Don't bother looping in the script and creating a file that you cat from yet another loop. Just run this from the host:
${execpi 10 ssh root@{HOSTNAME} -C /path/to/this_script}
Then "this_script" on the VM:
#!/bin/sh
# if /tmp/info_updates is absent or older than 60 minutes, (re)create it
[ -z "$(find /tmp -maxdepth 1 -name info_updates -mtime -60)" ] && checkupdates | wc -l >| /tmp/info_updates
cat /etc/hostname
if [ -f /tmp/info_updates ]; then
read n < /tmp/info_updates
printf '$alignr${color green}%d Updates$color\n' $n
fi
printf '$hr\n'
free -h | awk '
NR==2 { printf "RAM ${color grey}%s / %s$color ${goto 275} ", $3, $2; }
NR==3 { printf "SWAP ${color grey}%s / %s$color\n", $3, $2; }
'
printf 'CPU ${color grey}%d%% / %d Threads$color ${goto 275} ' $(awk '// { print $2 * 100; }' /proc/loadavg) $(nproc)
df -h / | awk 'NR==2 { printf "HDD ${color grey}%s / %s$color\n", $3, $2; }'
Last edited by Trilby (2019-10-18 01:47:39)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
@Trilby, thanks for checkupdates, didn't know about that. Could be I'm missing something obvious but in my simple opinion that script wont work as you expect.
You see, it counts lines after you check for 'mtime'. So in this case you would only see how many updates there are once every 60 min and the line count before checkupdates if you want to see it's output every 10s. Now I'm not into Conky so I have put the logic in2 shell functions with a little add in the form of 'grep' so the output can mean something more, instead of there being a number updates which doesn't say much, you can add a '+' sign behind the number if there is an update you're interested in, see my example: mind the line breaks!
'mtime' should be 'mmin' for min., but I think you meant tha
_upd() {
[ -f '/tmp/info_updates' ] && awk 'END{print NR}' /tmp/info_updates
[ -z "$(find /tmp -maxdepth 1 -name info_updates -mmin -60)" ] \
&& checkupdates >| /tmp/info_updates
}
# if an update of interest, put '+' sign after number of updates(former function)
_ifupd() {
[ -f '/tmp/info_updates' ] && grep -Ew 'apache|bash|linux|mariadb|php|systemd' \
/tmp/info_updates && echo +
}
Tested on tmux statusbar
edit: add check
Last edited by qinohe (2019-10-21 21:09:38)
Offline
it counts lines after you check for 'mtime'. So in this case you would only see how many updates there are once every 60 min and the line count before checkupdates if you want to see it's output every 10s.
Exactly, that's the point. Checkupdates doesn't need to be run every 10 seconds. It runs every 60 minutes, and the number of available updates are stored in the temp file.
There is no reason to store the list to the temp file every 60 minutes and count the number of lines every 10 seconds; that count couldn't possibly change. Instead get the list and count the lines every 60 minutes, and just read that number every 10 seconds.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Thanks for your input @Trilby
I'll definitly learned something about bash today and will integrate your ideas
Offline
..., and just read that number every 10 seconds.
Ah yeah thanks, see, I'm missing something obvious:(
anyway @flower if you wish you could still use that second function(_ifupd) if you have use for it;)
Offline
Pages: 1