LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Simple Bash script for reading size of Volume groupe. (https://www.linuxquestions.org/questions/linux-newbie-8/simple-bash-script-for-reading-size-of-volume-groupe-4175616290/)

Waris 10-24-2017 10:45 AM

Simple Bash script for reading size of Volume groupe.
 
Hey guys,

What is a simple bash script to read the size of a particular volume group and if that volumegroupe is at 50G big to send an email to user@mail.adress.com. I guess something like...

if storeage-device > 50G full;

then

>> send email to user@mail.adress.com

fi

I know that that is totally wrong in terms of how its written. but Something like that? My scripting skills suck :(

Thank!

TenTenths 10-24-2017 11:05 AM

Start with df and awk or maybe cut

For a great resource on bash http://www.tldp.org/guides.html and look for the Advanced Bash Scripting Guide.

Then post back here when you've something you've tried and ask for help with it.

Waris 10-24-2017 11:14 AM

Quote:

Originally Posted by TenTenths (Post 5773350)
Start with df and awk or maybe cut

For a great resource on bash http://www.tldp.org/guides.html and look for the Advanced Bash Scripting Guide.

Then post back here when you've something you've tried and ask for help with it.


on CLI if i do a #df /dev/mapper/volgoruptest

it gives me the size of that voulme group and that is part of what i am looking for but how do i implement that on to a script? that says if this volumegroup is at 50G big than send an email to the mail server..

TenTenths 10-24-2017 11:21 AM

Quote:

Originally Posted by Waris (Post 5773352)
on CLI if i do a #df /dev/mapper/volgoruptest

it gives me the size of that voulme group and that is part of what i am looking for but how do i implement that on to a script? that says if this volumegroup is at 50G big than send an email to the mail server..

Use awk or cut to get the used size and then compare that to the value for 50G (53687091200) and then use sendmail to send a mail.

Waris 10-24-2017 11:28 AM

Quote:

Originally Posted by TenTenths (Post 5773356)
Use awk or cut to get the used size and then compare that to the value for 50G (53687091200) and then use sendmail to send a mail.

I have a bit of a hard time writing that into script. Could you please write it for me?

TenTenths 10-24-2017 11:33 AM

Quote:

Originally Posted by Waris (Post 5773360)
I have a bit of a hard time writing that into script. Could you please write it for me?

Sure, $60/hour

Waris 10-24-2017 11:50 AM

Quote:

Originally Posted by TenTenths (Post 5773363)
Sure, $60/hour

haha good one dude. But ive been able to come up with something.


disk=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
size=50

if [ "$disk" -gt "$size" ] ; then
mail -s 'Disk Space Alert' user@mail.adress
fi

TenTenths 10-24-2017 11:55 AM

Quote:

Originally Posted by Waris (Post 5773369)
Quote:

Originally Posted by TenTenths (Post 5773363)
Sure, $60/hour

haha good one dude. But ive been able to come up with something.

Just quoting you what I'm normally paid to do linux admin stuff :P

See, wasn't so hard to go and copy some other script.

Oh, and by the way, your script is wrong, and not what you asked for.

Waris 10-24-2017 12:05 PM

Quote:

Originally Posted by TenTenths (Post 5773374)
Just quoting you what I'm normally paid to do linux admin stuff :P

See, wasn't so hard to go and copy some other script.

Oh, and by the way, your script is wrong, and not what you asked for.


You've gotta do what you gotta do. The main point to it was so i can get a preview of how it would look. I think thats been accomplished here. I just tried it on my test machine after tweaking it and it works great.

TenTenths 10-24-2017 12:10 PM

Quote:

Originally Posted by Waris (Post 5773379)
You've gotta do what you gotta do. The main point to it was so i can get a preview of how it would look. I think thats been accomplished here. I just tried it on my test machine after tweaking it and it works great.

So what did you tweak to get it to work properly?

TenTenths 10-24-2017 12:14 PM

The reason for asking is that the script you posted works on relative values (in your case % used) rather than the absolute value (50G) that you initially asked for.

Waris 10-24-2017 12:20 PM

google is a wonderful thing isnt it. But for your satisfaction here is the full script.

#!/bin/bash

File=$(df /somefile | grep / | awk '{ print $5}' | sed 's/%//g')

percentage=90

if [ "$File" -gt "$percentage" ] ; then

mail -s 'Disk Space Alert' user@mail.adress << EOF

free space is critically low. Used: $File%

EOF

fi

originally wanted to get the size of a VG but this will get the job done as well.

rtmistler 10-24-2017 12:26 PM

Hi Waris,

Great work!

If you're satisfied with the script that it solves your problem, please consider marking the thread as solved to help future searches see a solved issue they could possibly benefit from.

Perhaps also edit your last post to put [code][/code] tags around the code to separate it clearly and maintain any formatting.

TenTenths 10-24-2017 12:26 PM

Usually percentages are better anyway. Fairly similar to what I'd do except I'd have passed the volume and percentage as variables and fully pathed the commands to make it a bit more robust when used in cron

Congratulations on getting there though.

Waris 10-24-2017 01:45 PM

Quote:

Originally Posted by TenTenths (Post 5773392)
Usually percentages are better anyway. Fairly similar to what I'd do except I'd have passed the volume and percentage as variables and fully pathed the commands to make it a bit more robust when used in cron

Congratulations on getting there though.


Thanks, i just needed something imediate for now. But that is a good idea, i will disect this code on my own time to make it more robust like you said set variables and see if i can make it simpler. thank you for your input.


All times are GMT -5. The time now is 11:53 PM.