http://qs321.pair.com?node_id=1182422

vikas330 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm new to perl, so maybe I'm asking a simple perl script but for me its typical.

Please help me with a script for getting alerts on mail if FS utilization on machines exceeds 90% utilization and the output should be in tabular format.

I have the command to list all the FS having more than 80% utilization but I'm unable to print those outputs in a tabular format by writing perl script,

 $ssh $u@$i "df -P | /usr/bin/awk '0 + \$5 >= 80 {print}'" >> $FILE output to a file then I want to list all those FS with fields like

Server_Name FS_mount_point Utilization(in %)

and print all those in a mail in tabular format with fields like above. Thanks

Replies are listed 'Best First'.
Re: multiple machines disk space alert
by thanos1983 (Parson) on Feb 21, 2017 at 11:58 UTC
      Hello Thanos1983, Thanks for quick reply and suggestions. It really helps me a lot. But I need to print the output <need to check FS on mutliple machines and include all FS having more than 80% utilization> in tabular format in mail. So can you please help me in that. Thank you so much.

        Hello vikas330,

        Let's take the questions one by one:

        <FS having more than 80% utilization>: The link that I provided you (Perl script to monitor disk space and send an email) provides you the ability to set your own utilization limit. Sample of code based on description from link:

        # warning level 10% my $warning_level=10; # compare free disk space with warning level if ($df_free < $warning_level) { my $out = sprintf("Send an Email - Disk space on $dir => %0.2f%% (WARN +ING Low Disk Space)\n",$df_free); print $out; } else { my $out = sprintf("Disk space on $dir => %0.2f%% (OK)\n",$df_free); print $out; }

        In your case you can set the value to 20 instead of 10, this will mean that if the disk utilization will reach 80% then an email will be sent to you.

        <need to check FS on mutliple machines>: Regarding the multiple machines, here you could have to options. First and simplest option, apply this script on all machines you want to check the FS utilization on a cron tab as described on the link. Sample of description from link:

        You can run this script as a cron job: @hourly /path/to/df.pl

        By doing so each machine will email you separately in case that you have exceeded 80% utilization.

        In case that this is not what you really want to have as I suspect, then you need to do a bit of coding. I would write a script that can ssh on all machines and apply df -h, df -P or install the Filesys::DiskSpace which I find extremely useful and retrieve the data and compare them based on your threshold value (e.g. 80%).

        I prefer to use the module Net::OpenSSH::Parallel which can create multiple parallel ssh connections and retrieve all data. I have some samples of codes into an old question of mine (Best module to execute administrator commands on external operating systems (Linux)) where you will find samples of code for (Net::Telnet, Net::SSH, Net::OpenSSH and Net::SSH::Perl) if I remember correctly all will do your task executing bash commands remotely through ssh and telnet.

        In case of multiple machines you can create a simple loop where you ssh each machine separately and and collect data separately.

        As I said as up to you to play around and see what fits your criteria and how much you want to code.

        Regarding the tabular format hippo provided you multiple links where you can read and implement.

        Hope this is a more complete answer to your question.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
        Hmm, putting data into tabular format is easy. Getting it right in an e-mail at the other end might be more complicated, especially if Outlook (or another mail reader) changes the font or unilaterally decides to reformat it.

        Maybe using HTML mail format is the way to go.

        Update: fixed a typo.

Re: multiple machines disk space alert
by thanos1983 (Parson) on Feb 22, 2017 at 14:46 UTC

    Hello again vikas330

    I found some time and I put together 80% - 90% of the work of your request.

    Attention!!! in my example I am using ssh key exchange, in case you are not using that uncomment the psw on add user (password) and comment out the (key_path).

    Here is the code to do that:

    If you open the file 'output1.txt' you will see something like:

    If you open the file 'output2.txt' you will see something like:

    Next step is to create a function (sub) to open and process the files that you have created, I will leave this part for you.

    Update: adding mkdir in foreach loop and dir in %hosts.

    Update 2: adding open file and parse data into hash. I could resist of printing something as an output. Alternatively someone has to check the file(s).

    Hope this helps...

    Seeking for Perl wisdom...on the process of learning...not there...yet!