0

I'm currently running Arch with i3 and polybar. I also maintain 6 servers, so to make my life easier I made a series of shell scripts to notify me if a server is down.

The scripts work, but it's far from an elegant solution. At the moment, I have 2 scripts for each server. One that writes to the status bar (polybar) and one that writes to a log.

This will write to the polybar:

 #!/bin/bash

 srv=`ping 000.000.000.000 -c 1 -W 1 \
  | sed '2q;d' \
  | awk 'NF>1{print $NF}'`

 fail=`date '+%a %H:%M:%S'`



 if [[ $srv == ms ]]; then

     :

 else

     echo " 000.000.000.000 | $fail "

 fi

This will write to a log file:

 #!/bin/bash

 srv=`ping 000.000.000.000 -c 1 -W 1 \
  | sed '2q;d' \
  | awk 'NF>1{print $NF}'`

 fail=`date '+%a %F %H:%M:%S'`

 log1=`ping 000.000.000.000 -c 1 -W 1`

 log2=`traceroute 000.000.000.000`

 if [[ $srv == ms ]]; then

     :

 else

     echo "<---------------------------------------------->
 000.000.000.000 | $fail

 ----------------------------------------------

 $log1

 ----------------------------------------------

 $log2
 <---------------------------------------------->



 " >> ~/.log/ping.log

 fi

Both scripts are added as modules in my polybar config.

I tried having both of these scripts merged as one, but that would make the loading time for the error to show up in polybar too long, so I separated them in to two separate shell scripts.

I added the traceroute to see where the request fails.

I know there has got to be a better way to solve this. A total of 12 shell scripts is just way too much and it's really unefficient. Does anyone have a better solution to this? One that require less shell scripts and will still log in an efficient way?

  • Do you need a separate script for each server? Would a script that takes an IP as an argument and checks that IP not work for your use case? Also, are you using the output of the logging script in polybar or do you just use polybar to call the script periodically? – Patrick Ziegler Aug 14 '18 at 8:26
  • No, I actually prefer to have one script, but I'm fairly new to bash and I haven't found a way to make this work with only one script. And yes, the output appears in my polybar if the server is down. – discipulus Aug 21 '18 at 7:52
  • If you can generalize the functionality of your scripts to work on all servers, you could already cut down the number of scripts from 12 to 2, so one script that produces the output for polybar and one that writes to the log file. And as for the second question, I think my question wasn't clear enough. What I wanted to know is whether or not the second script you posted outputs to polybar or not, because as I see it, all its output go directly to a log file. – Patrick Ziegler Aug 25 '18 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.