Disk space check status no matter what you are doing in I.T..
This is one of the issue that everybody experiences whether you are a database administrator networking
guy a Linux guy or a Windows guy or working in any kind of application capacity are you always going
to get to this point where you'll have a disk space issue and this space issue either on your own computer
on a laptop or a server that you are working on where you would have to actually tell the administrators
hey can you free up the disk base and the administrator will eventually go and free up displaced by
deleting disks anyway.
System administrators or engineers who are actually managing the servers whether it's Windows Linux
or any other operating system.
They have to be vigilant of all the system health and biggest one of the main system health that you
have to check is that dissipates if the disk base is out it's 100 percent then your system will become
unresponsive.
And sometimes what happens is you know you want to delete a file but it's too late because you cannot
even log in.
The system is not letting you in so you have to be proactive and make sure your display's does not get
to 100 percent.
So that's one of the real life examples that everybody would face.
And I'm going to put a screw up for it and then you will automate that and we'll make sure this is not
going to happen in anybody's professional career where they would have to face the issue of space so
what we'll do is in this lesson as well first find dispells using the DFA command.
Of course there are different commands as well.
But the most important command and mostly used command is DPF.
You could also use you to find a disk usage base on a file system as well.
But is will give you the file systems usage on partition level and this is what we're looking for.
So we'll run that command then what we'll do is we will run different scripts that will filter output
and compare.
So it will run the same script DFA command and then we'll put the filters around or like grepped this
or that call them and then do the compares and if this column is greater than 90 then let me know.
Or if it's less than that then what do you want to do if then statement.
So there are different ways we're going to do that compare comparison.
This is a longer way to do it.
It's always been done like that but now and at the end of it also show a simple script which will actually
go and and it's like a one liner script and it will actually tell you that space and that all should
the end.
So let's go into the Linux server that we have
OK I have this terminal open and for disk space you would have to be some room.
No I don't think so.
DFI S.H. Yes you could run that command without being rude.
So that's why you don't have to be rude.
Just make sure you are in P.S. directory which is my home.
So piaster actually that's where I'm keeping all my scripts.
OK.
So the first thing first.
What do you want do you want to run the command of lines.
And the status that you will get is not from the size not from used now from available but from the
usage column.
If the usage column is reaching above 90 or 95 in some companies then it's a problem and you should
be aware of that.
And if you do not have some kind of monitoring application deployed in your systems then you would have
to do it manually.
And sometimes the systems do not behave normal when you install a monitoring agent on it.
Then you have no luck but to create an automation tool yourself like you're going to do here in this
lesson.
So we will focus on this column which is one two three four fifth column and we will compare our threshold
threshold goes above 90.
Let me know complain if it stays below 90 and perfect just keep running it and keep doing your job.
OK.
So for that we know that now as you could see the first thing we have to do is D F minus H.
And now we have to get rid of these temp FS partition.
These are Soie space and you don't want to get any information on that.
So you will do e-group minus the T MP s or dev t p FS.
So I don't want to filter out everything that has temper first or dev temp up office now.
And now you will see that you have file system right here only.
The one that you are looking for now you could also get rid of the first line if you want to.
So let's see if I could get rid of the first line and the first line is file system size used.
All that stuff and Teyl minus and plus two
and has gotten rid of got rid of the first line.
Perfect.
Now we have four partitions route as D-B one as DBI as da one and Viji Oracle.
Now you probably have a different file system in your environment and I'm sure you do.
Noctis probably because your system is probably built of course your system is actually built in a different
way or a different partition.
So you will see something else on the file system being mounted and the actual file system.
But I'm sure you are following the what is what I'm talking about how we could get rid of the stuff
that we do not want.
OK.
So that part is done now.
What do we want to do we want to get only the fifth column right because fifth column is the one right
here.
This is the column that we wanted to do a comparison on.
So we'll do.
Awk print call.
Oh yeah.
I accidentally put percent sign it should be dollar sign.
OK.
This is not a right column I'm looking for.
Oh I made a mistake for a column and want to replace it.
Now we know 70 to 16 and two.
OK.
We are here up to here perfect.
Now if we are comparing these integers with our predefined numbers let's say we wanted to get an alert
if the value is above let's say 60 and then we know the value is 70.
But it has this percentage sign.
So when we do the comparison with if else statements this is going to complain and scrip won't run so
we have to get rid of the percent sign as well.
So to get rid of that as well as use the cut command cut minus the and unwanted carot which is percentage
code.
Single code space minus F 1
and you will see.
OK let me make this bigger so you will see the entire command
and up arrow key.
So the command filter filter or the first column first raw filter out the fifth column and cut and get
rid of percent sign.
And once you get rid of 5 percent and give me the first field and first few is number one field which
was right here and then if I put f two it would give me just percent of you want one.
So we got up to this point.
Perfect.
This is what exactly we want to get out from DFA.
And so we could do our comparison.
Now what you would have to do is now you create a script and do for loops so let's say you did this
and you copy it and you create a script called Check Desk
and you give defined the shell
or the
date
description
this script will check for disk space
modified.
And I could put down right here.
OK so this is going to give us all the values in the fifth column of the DFA command and it will filter
out the percent sign.
So if I just run the script like that check disk
the check disk or I put in a variable that's why I didn't run so this take other variable and just stick
in the command by itself.
And then to check disk you'll see it as giving me or the entire column and four rows.
OK good.
So now we know we are getting the result that you want.
Now we have to give this entire command and assign a variable and give the variable name a.
And if it's a command you put a tick which is right above sorry which is right below the escape and
you close it the entire command would tick as well.
This will show that this is a command not just a value.
So now what we want to do now we have to use the for loop we could use you could do this first like
if
dollar a is greater than let's say 50
then go check disk space
else you don't want to do anything else just feet
and done.
But it will not run.
Let's try run it see it didn't run.
OK.
The reason it didn't run it too many arguments you see it's right here too many Ogwyn why too many arguments.
Because when we run that line this command it gives us more than one value.
Remember Gabes it gave us four rows with four different values.
So if it has given us only one value that say if it has given us just wrong first rule and number is
40 then it will work.
But since it has four rows then we have to use for loop for that.
So so for loop can go from first row second row third row for trial and then done once it's finished.
Looking at every roll.
So that's why just for single if is now going to work we have to put a for loop around it.
So how we do it we do four and I'll put as invariable in dollar a dollar a dollar a is this variable.
So what I'm saying is whatever the result is coming from this line put it into another variable and
that variable is I OK I put it in.
It's it's in the variable it's in the container.
Now what do you wanna do now.
I want to do is if that container is dollar.
Now dollar a.
If that container has a value anything above 90 percent which is 90 then go check disk space fee and
then done because we are doing for loop.
Sorry I missed one thing.
Four also has two for i in dollar a.
Do what.
Right.
I missed that do.
If.
Which is finishing up with fee and do finishing up with done.
Or effect.
Now when I run the script it is not going to give me anything why.
Because I have
defined the value as if the output of this command any of the output is above the number 90.
Then tell me that check MySpace but none of the output tech here.
None of them has 90.
My file system is the highest one is 70.
So I have to pick a value that is below 70 so this will be Kabera by our script.
So instead of 90 pick 50 so now this should work.
Now it should give me this check disk space.
So to clear the screen check disk go see says check this space because it did find one of the values
that is above 50 and it complained.
Now we have to be a little more.
We need more information from it.
That's it not just check display but tell me what.
Which partition.
Right.
So if I have this four partitions that I was looking for the cat my script and let me run this command
again.
And you'll see I had this for partition go this one as well.
I have these four partition and it gives me if I run my command that just check disk.
It gives me check this space because it actually met the value I have defined.
But it only gives me information that check this space but not which display.
So in order for me to be more informational about this error I have to with the mapped drive as well.
So how can I also get the map drive information where I could do is I could come in here and say echo
this base check this base and I'll put a dollar I like which displays it this is dollar eyes only going
to give me the number.
OK.
Nice.
I have the number to check the number numbers.
This green checked this check disk space seventy.
OK.
So now we know that there is a space a disputation with up to 70 percent.
Now we also need more information which disk partition.
All right no problem.
We'll keep working on that.
So for that what we have to do is we have to grab that dollar from the DFA command.
So if I put in Bryde here could do tech because again this is a command D F minus H grew up for dollar.
And take a close.
So now Echo will echo this.
And if I don't put an tick right here then it will read out the entire echo with DSF my ass up.
Let me show you know what.
So this way you know what I'm talking about so let's say if I take a tick and run the check desk you
see it is giving me deifies age as if it's actually wanted to put the screen.
So that is why I have to put a tick around it.
And clothes of a tick.
Now I should have the exact information them.
So we checked base 70 which is on file system sento us Dasch route and space and untired thing and it
is mounted on slash.
So it's basically grepping at that number and of course a great number exist that's why I'm going to
get that back.
So anyway this is the for loop that you would have to use to check the displays.
Another way we have the same script written a different way.
I actually got the script the second script from online.
I was looking online and this was the script that I found out initially.
So I had already created the script and I what I did is I copied the Create disk file.
Sorry.
Check
Disk and I copied it to one and I had to know Saddam.
I'm going to go to the second one is check disk 1 OK.
And this one you will see it is slightly written differently but it is doing is running command F minus
H uppercase H which is pretty much the same as the lowercase H if you wanted to see what man D F has
with minus H uppercase H.
Actually this is likewise but use powers a thousand not one thousand 1024.
So it's pretty much the same thing.
So we leave it as is grep e.
It is trying to filter out anything that starts with file system temp CD-ROM or dev FS is doing the
same thing then awk print dollar one dollar five dollar one which is the fifth column and first column
so when I run this command.
Just that in my case or the terminal
I have to put a db of mine saying before that if my uppercase H and now I run the command and I'll get
that output.
So pretty much the same way I did.
Now difference is that they instead of using the for loop they're using the why loop.
So in Wildblue what they're doing is there while read the output and output as this entire command.
So put the output of this entire command into the variable call output and then do and they're defining
another variable in the variable they're doing is echo the output.
And all can get and printed dollar one which is the file system cutting it and getting a percent sign
if there's any to it.
And no sorry but the output they're trying to get rid of the Fifth Column per cent sign and putting
it for one second variable the defining is Ecko the same output which is the upgrade Herodias a variable
and AWC and print d'Albret to have a little space here.
I'll just get rid of the space OK then if the use percentage which is this variable is greater than
90 then echo running out of space partition which is this partition variable on hostname which is the
hostname of this machine as on date which is today's date and fee and done.
So this is written a little differently.
And let's try to run the script
this one it didn't give me anything because the the set value
is 90.
So I'm going to change it to 50 and now Iran it and you'll see it as giving me this entire line running
out of space on this file system 70 percent on my first Linux as the host name as on September Saturday
and the date and time and everything because it has that date in it too.
You could use that one if you want to.
It's a little bit complicated but if you do understand this script perfect be my guest used that one.
It works perfectly as well and is not using for loop as I was using it in my previous script.
So now you have to script.
So the third script that I want to show you guys which is very easy and simple and straightforward.
I'm going to copy that.
Check desk to check desk 3 the checked disk 3 and that's get Ridulph
everything.
And what this was going to do is F-minus H.
AWC sorry AWC.
Single code zero plus dollar five greater than or equal to 10.
This is the threshold that I want to go with and
that threshold OK if I just run this command just like that
Bucco is giving me everything that is above 10 percent on the fifth column.
So what I'm saying is the fifth column has to be equal or greater than 10.
So these are the equal and greater than 10.
So if I modify that to let's say 50 then it would give me only one of them.
One of the rows which is the slash mounted
so put to put it in a script.
So what we'll do is we'll to echo this a simple line.
The Echo following is the disk space steps at go one more line and then it will do is run that same
command.
Right here I'll get rid of this
and I will filter out what I don't want and will just print dollar 5 and dollar 6
and that's pretty much it.
This is just that simple script what it's doing is it's running a D minus ish man walking.
The fifth column and saying this is just the way it is zero plus dollar 5.
And if the dollar five fifth column is greater than or equal to 10 then print that and all fifth and
sixth column.
So let's run the check.
Disc 3 and following is a displaced data 70 and 16.
Perfect.
Now since I have the threshold set to 10 I will modify that
to 90 or whatever so since the last one I have 70 so I'll go with 60s.
Anything above 60 I should get a message
and it says The following is the display status 70 percent.
And this is how I'm going to get the result when I run this command.
I could change the value to which over the value I want.
So this the last grip that I have written this is the one I've been using lately because it's very much
simpler than meekly the screen very much simpler.
And this one liner.
You don't have to use the for loop.
You don't have to use the while loop you could just simply use this command to check the display status.
So now you learn how to do how to check the displays for loop how to check the display the why loop
and how to check the display simply by one line.
Three different options try every every all three scripts and see if you are actually having any issues.
And I'm sure all three of them should work for you.