range=$(seq -f "ma%04g" 5001 5505)
  for n in $range;do
      ping -q -c 1 -w 3 -s 10 $n >/dev/null
      if [ $? -eq 0 ];then
          awk -F, -v key="${n}" -v val="up" -v OFS="," '$1==key{$2=val}' /tmp/master.csv > /tmp/temptest.csv
          cp /tmp/temptest.csv /tmp/master.csv
          awk -F, -v key="${n}" -v val="down" -v OFS="," '$1==key{$2=val}' /tmp/master.csv > /tmp/temptest.csv
          cp /tmp/temptest.csv /tmp/master.csv

Not sure if I raised same query anywhere in this forum but just wondering how can I speed up this process with xargs or parallel so that I get the results. Also will it makes the /tmp/master.csv file empty?


Gnu parallel would be easily able to run the loop, however, you shouldn't modify the CSV file at each iteration. It's better to collect the output into a new file and then merge it into the existing CSV, or just write the whole new CSV file from scratch if you don't have node-specific data in it.

do_one() {
  if ping -q -c 1 -w 3 -s 10 $n >/dev/null ; then
      echo up
      echo down
export -f do_one
seq -f "ma%04g" 5001 5505 | parallel -j0 --tag do_one {} > results
# results contains host \t up_or_down
# create files with only hostnames for up
grep up   results | awk -e '{print $1}' > up-hosts
# create files with only hostnames for down
grep down results | awk -e '{print $1}' > down-hosts
# This perl script takes 1 second to run if you have 100000 hosts
perl -i.bak -ape 'BEGIN {
        # make fast lookup hash for up/down hosts
        chomp(@up=`cat up-hosts`);
        @up{@up} = @up;
        chomp(@down=`cat down-hosts`);
        @down{@down} = @down;
      sub updown {
        my $host = shift;
        my $last = shift;
        if($up{$host}) { return "$host up" };
        if($down{$host}) { return "$host down" };
        return "$host $last";
      # $1 = hostname, $2 = up/down of master.csv
      s/^(\S+)\s+(\S+)/updown($1,$2)/gme;' master.csv
  • master.csv "node state core memory site ma5001 up 72 128 london ma5002 down 40 64 goa" – ARthur Aug 2 '20 at 11:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.