CPU, Memory, Disk Utilization Report from Multiple Servers Shell Script – Tech Arkit

CPU, Memory, Disk Utilization Report from Multiple Servers Shell Script – Tech Arkit


for more updates click on subscribe and
click on bell button for latest notifications hi friends welcome to Tech Arkit YouTube channel my name is Ravi in this video session we are going to see
that a small shell script which can be used to collect multiple servers CPU
utilization memory utilization and / disk space utilization so this is the
script which is going to be run into the multiple servers then collect the data
into the single file where we can see the always single output in a single
file like share comment and subscribe so how you can achieve that so we are going
to create a small script which can go and execute in all the servers and to
get this output and write it on the file so let’s go ahead and see how we can
write this script so first of all I am just going to create some directory called
scripts in this script I am just going to make one script file so which I
may call as cpumemdisk.sh file so in this I am just going to make the so
here maybe can read the description to collect CPU memory and disk usage
report from multiple servers ok so first of all to execute or these commands in
your remote machine you have to create the passwordless authentication
so let’s see this how you can go and go ahead and create that passwordlesss
authentication out of the script creation so here
so the author name would be ravi kumar and website you can visit for this is server-computer.com so now the script is going to be collect the script format
the useful format is going to be like this so my formatt is going to be
get something like this first Host Name okay and the next one is date and time next one is CPU utilization next
one is mem utilization and last one is disk utilization so this is what I
would like to collect from the remote machines and store it in a single file
so in order to do that first of all I am going to define a few variables so the
variables is hostname first of all to correct the host name from the remote
machine and the command is hostname which can collect the host name from the
machine in the date I am just going to define date and time in this way the
date can be used the command to collect the date so here I am just going to use
percentage %Y for year find percent a small %m for month and %D for day and I am
just going to collect hours minutes nine seconds so this is the date form date
and time format the next one is CPUUSAGE so this CPU usage I’m just going to
collect using the top command so that is in a real-time so in top command if you
want to query that top command results you have to run into the batch mode and
I would like to run two times from this two seconds from that batch and the
-d delay time one so from here I just would like to get the first of all I would
like to get the CPU so where it is Cpu(s) something like this so if you type this
command example say that this command so you will get the first
one the like this you will get the output from the top command from this
actually I want this portion from that so I want this one so this is the actual
CPU utilization from the CPU so in order to get this one so you can do that like
now take the first line over here and then cut this value from here okay so
maybe you can also try to use one here we get only the first value from here so
like this now I am just going to use this value as a one okay now I am just
going to cut this value using the awk so I want the second value from that
so print $2 I am just going to print the second value out of it from
that I am just going to divide that value so the value will come something
like this dot separated by that DOT so I want only the first value the second
value is having a little value out of after the dot so that I am just going to
do use the delimiter dot here and I’m just
going to print the value after the dot is first value so this command will
print the exact the CPU usage and I’m just going to use a define one more is
that many usage the mem you cannot get directly is that person in percentage
but if you want to calculate that percentage into into percentage you have
to do the usage value total value do you into one hundred like the percentage how
you calculate the percentage that the maths your going to apply over here
so in order to do that I’m just going to apply that value here is that print
dollar $3 to in two hundred so this is the one I’m just going to apply or here
this one will get the memory usage percentage so if you type this one you
will get this percentage so the memory utilization currently is 15 percent if you
can see this example so free grep mem and I’m just going to print the
third value or here example say that print dollar three so this is the value
and printer dollar to this is the values so if you type free the value is or here
is this one and this one so this one used and total so used to divided by
total equal to percentage so I’m just going to convert that into percentage
over here so that one we’re going to give me
the percentage over here so now the final one is DISK usage so how
you’ll get the disk usage so here if you use that it will be df -h
that’s going to give sometimes if you have a lengthy disk name so that’s going
to give you a different format so in order to avoid that kind of formatting
or the different value out of it you can use df – capital P so example say
that if you go df – capital P it gives you the value something like this or if
you use df -h uses will do something like this okay like this
otherwise you can use different HP also that that will also give a went to
different human readable but anyway we are going to use only the percentage so
that does not matter if you use h or if you does not use H it does not change
much so so now I am just going to use capital P or here and in order to avoid
confusion are the the formatting errors so I’m just going to use column – T
which is going to add the text with the tab delimited values and now I am just
going to cut the value is print dollar five dollar five is any usage value so
from here so I’m just going to say there is a one when you called the header the
filesystem something like that so out of that I’m just going to cut that and I’m
just going to replace the percentage value the percentage whenever you cut
the this one example say that this command which gives you the percentage so this gives you the percentage over
here I am just going to cut this percentage from here using the sed
so they said we’ll replace your percentage with the blank space which
means that which will be the percentage will not appear so it’s going to be
disappeared so now I am just going to set the value over here the farm at 1 so
maybe the same format I’m just going to use it over here for the header maybe
you can use something like this or maybe you can use something different like
this like this so the head that may be
something like this for fancy stuff so I’m just going to pass this as a single
value because this has some special characters over here to not execute on
the shell so that’s why I’m just going to write this out put somewhere else on
this opt maybe a say usage report something like this and I’m just going
to print above values over here so dollar hostname over here and comma
separated value and the date over here comma separated value and CPU usage over
here similar memory usage was here and this gives edge over here and I am
sending these to this report okay in similar vein I’m just going to write the
same similar commands for executing on the remote in your loop because I wanted
to execute this in a loop because I have lot of host to configure yeah I want to
run in multiple host so I’m using for loop over here so far I end I’m just
going to use cat I’m going to define the most list in script file say host list
and I say do the similar commands I’m just going to define on the pillow so I
just copied this here but a little different like I’m just going to define
something like this because it’s a remote okay so now I’m just going to print all
these values so host name date and time and CPU usage memory usage find this key
usage so here I’m just going to define that SSH because I’m going to do a SSH
into the hi host I’m just going to execute that command and come back so
here maybe I can use SSH dollar I and in space it won’t accept actually in a
command so that’s why I’m just passing that into the single quote SSH dollar I
the same command I’m just going to execute into the remote host and collect
the information like this so now the same commands I’m just going to execute
into the remote host and collector info over here and bring the same values into
the report usage report so this is the usage report over here alright done with
e for loop so now I just used if our loop and running the same commands into
the remote host and get the output and store into the variable the variable
will be printed and stored into the file so this is what the script is and let’s
go ahead and first of all say my host wish we have to create the host list so
this is the host list here you have to write a theater sauce DNS names so in my
case I am not using and so that I’m just going to write my
host list or here I have to host one is 2.50 on its 2.10 so now in order to
create the password list connection you how to use SSH key gen – t RS a so this
is the command so now it will generate the key file in dot SSH ID underscore
RSA dot table this is the key so this key you have to copy it and paste it
into the remote host so now I’m just going to connect to the remote host 192
dot 168 2.50 and say yes and the password and now you just verify that
whether you have the dot SSH are not so happy here there is no dot as a such a
directory maybe we can try to generate the key over here but which is not
required just first creating the dot SSH directory so here in this you have to
use authorized underscore keys file and paste this content over here now I get
from the host and verify that you are able to connect to the remote host
without any password ok 2.50 is I am able to connect now try to connect to
192 168 dot 2.10 ok and you cannot you have to set up so here also maybe you can use just to
create the dot SSH file dot SSH and here you just use authorized on the scope
keys I knew how to just pace this keep I liked it from the host and try to login
so it’s logging in now just go to these scrapes and execute that script now cat
this usage report you can see this this is the report what you want so first one
is the host name the host name is this one and remote host name is this one and
it’s CPU utilization current rate is twenty one percent and memory
realization is zero zero okay and the memory percentage is one over there and
the memory utilization for the remote host is fifteen percent twenty one
percent and this one is 12 percent and the disk is a zero percent here it is
hundred percent here it is 25 percent so like this you can get the CPU
utilization memory idealization and discretization in a single screen as a
single report if you want to add a multiple horse you can add it then every
day can collect this report to this usage report so later later point of
time if you want to combine this report as a usage report and you can run most
of the times so that it it can give you the
a graph picture graph when you put into the excellent run some graph so that
they can say that okay this day civilization this one in relation this
one and descritization with this one something like that okay that’s about it
guys thanks for watching stay tuned please subscribe to the channel for more
upcoming video signed courses please like comment and share please follow us
on social networking site if you have any technical related questions you can
ask

7 thoughts to “CPU, Memory, Disk Utilization Report from Multiple Servers Shell Script – Tech Arkit”

  1. Bro can you drop here wmware workstation 14pro and compatible Ubuntu (Linux) links drop please.i have already installed on my pc wmware workstation 15pro already installed because I don't know which compatible this.

  2. I'm beginner to learning Linux
    Can you share from beginning all videos master link/playlist link.

  3. what is the d1 ( d=> is delimiter and what is the "1" ) command .can u please explain me.And what is the batch mode

  4. In the below example I want to only display UID and Process Name in the “Access.csv” file which consume MAXIMUM Memory. Write a shell scripts for the same.
    Access.csv

    UID Process Name PID MEMORY
    1, A, 11, 90%
    2, B, 22, 30%
    3, C, 33, 115%
    4, D, 44, 45%

Leave a Reply

Your email address will not be published. Required fields are marked *