So friends, as we know, one of the most important things that we have available
to us on our system
is the available RAM, i.e. the amount of memory that we can use. Without this,
well, our system is
going to come to a halt and it's going to begin to perform really, really slugg
ishly. We might
have to use things such as swapping, which is where we invoke the hard disk to
be utilized as virtual
memory and the event that memory is running that low. So what we want to do is
to utilize
Ansible to run some of the more common Linux commands that we can use to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Ansible to run some of the more common Linux commands that we can use to
ultimately diagnose
and monitor when the memory happens to be running low and even perhaps
identifying the processes
which are consuming the most memory. Once we do that, we can run this playbook,
target all of the
devices in our network and get a quick snapshot of the memory utilization for
each device in our
managed network. So as we know, there are many different commands that we can
use to see our
outputs. We can use the free command or we can use things such as the top
utility. All of these are WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
utility. All of these are
valid. In the case here, I'm going to elect to use the free commands. So with
that said, let's run
that command and see the output that we are getting. So we just say free. And
right now we can see
we have information relating to the actual memory as well as the swap space.
Now, like I say,
the swap space is really just talking about the virtual memory. Okay, so this
is not really what
we're going to target right now. This is only when the hard drive is invoked in
the place whereby
our memory is running low. So really, we can just effectively grip for the line WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
our memory is running low. So really, we can just effectively grip for the line
that says memory.
Okay, so let's do this right here. If I say free and I grip for the memline,
notice the way we are
effectively filtering out the bottom right here. And as we can see here, we
have the first column,
which is just the main itself. Then we have the second column, which denotes
the total memory.
And then we have the used the free memory that shared the buff cash out as well
as the available
memory. Now if you so wish, you can see this information and probably a little
bit more readable WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
bit more readable
form if you do free dash H. Also give you the information at a glance. But for
our purposes,
I'm going to recommend that we just use the free command on its own. This is
going to become a
parent when we build up the playbook pretty much we're going to be using these
raw values
to construct a percentage values. We don't want to be mixing and matching
potential different
values such as gigabytes versus megabytes, that type of thing. Let's just leave
it all at the same.
So we can see here at a glance, my total memory is 3.5 gig and the available
memory is two gig. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
memory is two gig.
Therefore, what I want to do is I want to see as a percentage of the total
memory,
how much of that memory is free. So basically, the last column, we want to be
as high as possible.
That tells us that we happen to have the most memory available. Now, how we're
going to get that
in the form of a percentage like I said, we're going to take the raw numbers.
So in my case here,
what I want to have is 2, 1, 2, 9, 4, 8, 8. And then I want to divide that
number by the total
memory, which in my case here is going to be 3, 6, 7, 2, 1, 7, 2, which if I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
memory, which in my case here is going to be 3, 6, 7, 2, 1, 7, 2, which if I
just calculate right now,
it's going to give me a value of round about 0.57. And then all I want to do is
I want to times
that by 100. And that is going to give me the percentage of available memory.
In my case here is
50, 7% available. So there are a few things at play here. I want to take the
value from
this particular column, the seventh column and divide it by the information and
the second column
and then times that by 100. So if you recall from your skills on learning Linux WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and then times that by 100. So if you recall from your skills on learning Linux
via the
great hat certified system admin certification, that is the very particular
utility that we can
use to parse information in this tabular like format. This is going to be the O
CH command.
So let's see how it actually operates. So let's clear the screen right now.
Here is the command
just gripping for the main. Now if I take this output here and I pipe it as
input into the OCH
command, and then I have my single quotation marks and then my double curly
braces and inside of the
curly braces, I'm going to say prints. And if I say dollar sign one, that acts WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
curly braces, I'm going to say prints. And if I say dollar sign one, that acts
as a placeholder
and tells me to print the very first column. What it's going to see here is
just the word
meme. This is going to be treated as a column. Okay. So that's the first column
. The second,
the third, fourth, fifth, sixth and seventh at the end. So if I happen to say I
want to see
the seventh column, I did dollar seven, now we're getting here at the end
column. The actual
value here has changed a little bit. That's because there are minor
fluctuations in the memory
utilization as I'm using my system. Okay. So these values will continually be WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
utilization as I'm using my system. Okay. So these values will continually be
in flux and
changing ever so slightly unless something major happens, in which case they'll
change greatly.
So this is the seventh column. And if I want to get the total, I would use the
second column,
which is going to show me 3672172. Now here's the cool thing. I can actually
run the calculation
right here directly. I can just say print dollar sign seven divided by dollar
sign two. Pretty much
take the information we see from the seventh column and divide it by the
information we see
in the second column and print the output. So let's try this right here and WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
in the second column and print the output. So let's try this right here and
look at this.
We're actually getting 0.58. We have a slight fluctuation. I believe my
previous calculation was
59 or 57 around about that. Anyway, this is what we're seeing now. We're
actually getting a little
bit too specific information in the case here. We're getting more than two
decimal points. Now
what I can do is I can specify to all that I want to have a float value,
including a very
particular amount of decimal points. So does very briefly, we have a difference
between an
integer, which is just a regular whole number such as the number four, or WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
integer, which is just a regular whole number such as the number four, or
perhaps maybe the number nine,
a floating point value would be including that decimal notation. So I could say
, for example,
nine point one two, this would be a floating point. And again, we'd be using
here two decimal points.
So like I say, as opposed to getting this value here, so if I change the print
statement to be
print F and then within quotation marks, I specify the amount of decimal points
I want to include,
I can say percentage to F. And now what I will do is I will have my comma here,
and then
be around the calculation. So if I hit enter now, we're actually just getting WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
be around the calculation. So if I hit enter now, we're actually just getting
back 0.58. In fact,
let me add a new line here. So it looks a little bit neater. So I'll say backsl
ash in and I answer,
this looks a little bit nicer. Now, like I say, if you want to convert this
into a full percentage,
we're going to have to modify the commands so as to times it by the value 100.
So let's try this
right here. Okay. And again, we're getting back the information 57.78%. And
again, if we just
wanted to see one decimal point, I could change that to one F right here. And
to enter, now we're
getting 57.9%. Okay. So whatever you want to see, if you want to see more WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
getting 57.9%. Okay. So whatever you want to see, if you want to see more
information,
more specific, you could say we want four floats, that will give us even more
specificity. So in my
case here, let's maybe go with dot one. Okay. So what we want to do is you want
to basically apply
this information and run it within our Ansible Playbook. So if I just created a
playbook called
play one dot YML, that's going to insert mode, have our free hyphens at the top
, give this playbook
in name, and I'll just say play one testing memory. Now, how am I going to
target? Let's target the
all groups so that will be all devices in my network. And right now, say the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
all groups so that will be all devices in my network. And right now, say the
gather facts to be the
billion value of false. Now I define my tasks. Now the task here is simply
going to be use the
free commands and parse memory percentage. So what am I going to do here? I'm
going to say Ansible.
I'm going to use the built in shell module. And all I want to do is if I just
go back,
and I just copy this particular command right here. And all I will do is I'll
paste it directly
into the playbook. Now what I want to do is I want to register this output and
let's just give it WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
let's just give it
the variable name memory percent. So right now all I will do at the moment is I
'll just print out
the memories. I'll just say print the output. And now I will use the Ansible
built in debug module.
And then I'll specify the message and I'll just say inventory host name to get
the host name of
the device. And I'll just say has and I'll grab the memory percent variable I
've just captured,
and I'll add a little percent design afterwards, and then just say the word
free. So let's actually
see how this shakes out them. So if I just say Ansible playbook, play one dot y
ml and to enter, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
ml and to enter,
open in fact what I want to do here is that I just want to print out the
standard out. So
let's just go back to the variable here and I'll just grab STD out, run this
again. Look at this
right here. We can see the blue box has 60.9% free and the yellow box has 60.6%
free. And again,
if I wanted to make that a little bit more descriptive, I could change it to
say memory
phrase. It's a little bit clearer. So for Iran, it's there we get the values
here. Now suppose we also
want to see the processes which are consuming the most memory. Well, if I do ps
dash e, this is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
dash e, this is
going to show me absolutely all my processes scroll on up to the very top, we
could see
absolutely everything. Now right now, this is displaying the information and a
numerical value
with the process ID increasing gradually we have process ID one all the way
down. So if I want to
modify the format, I can also use the dash o fly. Okay, so now what I want to
do is I want to
specify which information do I want to see here. So I can say I want to see the
process ID, which
shows me just the process IDs. If I also want to see the parent process ID, I
can add a comma and
just say PP ID, which shows me both of them. If I then want to see the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
just say PP ID, which shows me both of them. If I then want to see the
associated command, I can say
comma CMD, which shows me the command which is associated. And then I can say
comma percentage
of memory being used. So now we get this type of information. Now right now,
this is still sorting
things and numerical value, increasing via the process ID, what we want to see
here,
is we want to sort things via memory utilization. So if I say dash dash sort is
equal to, and then I
say percentage of memory, what it's going to do here, it's going to show me the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
say percentage of memory, what it's going to do here, it's going to show me the
usage of memory
and gradually increasing order. So right now the process is using the most
memory at the very bottom
and as you move on up, they use less and less memory. If you want to invert
that logic, we can say
sort minus percentage M, which is going to flip it. So now we get the ones that
use the least at the
bottom and at the very top, we see the processes using the most memory, which
in my case here is
the nome shell. Now we don't want to see absolutely everything. So what I could
do once again is to
use the head command. So I can pipe that into the head command, which is going WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
use the head command. So I can pipe that into the head command, which is going
to show me the top
10. And in my case here, let me just say I wanted to see the top five. So I can
say his head dash
n for number of lines and then say six, which is going to show me my column
here, which acts as
one value. And then the next five values are going to be the five processes
considering the most
memory. So let's take this custom command here, and we can add it into our
playbook. So let's give
this a name and I'll just say show the top five most memory intensive processes
. Again, I'll use
the Antibel built in shell module and just type in this custom command that we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the Antibel built in shell module and just type in this custom command that we
've crafted. And
then I can capture the output via the register keyword and I'll register the
output of this command
as the variable. Let's just say this as top memory processes. Then I can add a
new task and just say
prints the top processes. And again, we use the Antibel built in debug module.
And let's print
the message to my message will have my two quotation marks and my double curly
braces. And all I'm
going to specify is that I want to print out the top memory processes variable
I've just created WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I've just created
and I want to grab the STD out. So let's save this right here. And we'll just
say,
answer my playbook, play one dot yml and to enter. And as we can see here, we
get the output of each
device. We can see the amount of memory free. And then we run this particular
command. Now,
as it stands, the output here is super super ugly. So what I can do is as
opposed to printing
STD out, let me print STD out lines, okay, which is going to help us with these
new line characters.
Let's try this again, rerun the playbook. And that this looks a lot better. So
at a glance,
we can quickly see how much memory is free, as well as printing the top WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we can quickly see how much memory is free, as well as printing the top
processes on each
device here is the information here for the blue box. And below here is the
information for the
yellow box.