Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Automatic monitoring of memory and storage space under Linux!

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

It has been some time since the last update of the article, the editor can not update the article in time because of some trivia and physical illness. Today, the body gradually recovered and rushed to update the article. What Simeng brings to you today is how to automatically monitor some basic configurations of our server to ensure that our applications can run better and predict performance bottlenecks!

Today's main content includes, Linux's basic Shell programming process control statements, the use of cut, the use of grep, and the use of awk can achieve what we are talking about!

The following commands have different uses with different parameters. Simeng tells you some basic uses in order to achieve the functions mentioned. If you want to better use the following commands, you can refer to the documentation to try to experiment with different parameters!

1) first, let's talk about the flow control statement if of Shell, which is in the format of

If command;then

Logical processing

Else

Logical processing

Fi

The above note is that there should be a fi at the end of if.

2) the basic use of cut, which can use some regular delimiters to get a line

If we want to get a string such as for example, we can write: cut-d ":"-f 1 it means: use: split and then get the first line, and-d is followed by what symbol is used to split it, and the title is ":", of course it can also be ";" and so on, and the following-f is the column I want to print out.

3) grep is a powerful text search tool, for example, we want to search in a text.

For example:

We first create a simengphp.txt, and then we want to get the line of the string simengphp. We can do this.

So we can get the simengphp line.

4) awk is a complicated command, so let's talk about how to use it directly. Awk'{print $I} 'means to use spaces (irregular, cut should be regular) or tab segmentation by default. We will explain it with an example later.

All right, the basic commands are over, and what we need to do now is to write Shell scripts to implement our functions.

First of all, let's write a script to monitor memory, and we will use one more command: free-h

So we see the memory usage of the first row: what we want is the value of free to monitor, so we will get the value of this line step by step.

1) first show it all, and then we pass it through the pipeline to the next command to deal with.

2) We use the grep command to get what we want

3) through the awk command, we get the fourth column and print it out. $is followed by the column we want to print. Print is used with awk, and print cannot be used alone under linux.

4) then use M to split the value through the command cut, and we can get the value we want.

Above we just show how to get the value we want, and then we'll just start writing the Shell script:

We run it and return the value in else, because what we just saw is 204, which is exactly greater than 200, so there is no alarm operation!

As you can see, the function of monitoring memory is the same as that of monitoring hard disk space. I will script you directly.

This is how we automatically monitor memory and use hard disk space through Shell scripts, and then we can execute the corresponding scripts through scheduled tasks.

The script is very rough, and there are still some areas that need to be improved. I hope my friends can optimize it and monitor your server after studying it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report