Monitoring Linux systems with Python

Source: Internet
Author: User
Tags readable

One, the Python writing monitoring tool One, the multifunction system resource statistic tool Dstat 1, Dstat Introduction

Dstat is a multi-function system resource statistics tool implemented in Python language to replace commands such as Vmstat, Iostat, Netstat, and Ifstat under Linux. And, Dstat overcomes the limitations of these commands, adding additional functionality, as well as more counters and better flexibility. Dstat can display very comprehensive monitoring information on one interface.

The dstat will display the monitoring information as a list, and output it in a different color, showing the monitored values in a more readable unit. For example, for byte values, Dstat automatically displays the values in K, M, G, and other units, avoiding confusion and errors caused by the large number of values that developers use when using other commands. In addition, using Dstat makes it very easy to write plug-ins to collect monitoring information that is not collected by default. Dstat is designed for people to view monitoring information in real time, so the monitoring results are output to the screen terminal by default.

2, the characteristics of Dstat
Contains a number of extensions; 8 can be grouped statistics block device/     display different units in different colors, increase readability; 13 supports CSV format output for easy monitoring of Gnumeric and Excel to generate graphics. 
3. Installation and use
Yum Install-y Dstat

The--version option of the Dstat command, in addition to the version that shows the Tat, shows the version of the operating system, the version of the Python language, the number of CPUs, and details such as the list of plug-ins supported by Dstat.

Dstat--version

Dstat--list get dstat List of plugins

Dstat--list

Enter the Dstat command directly at the terminal, and Dstat will run with the default parameters. By default, Dstat collects CPU, disk, network, paging, and system information, and outputs it at a one-second frequency until we press CTRL + C to bundle.

  

4. Dstat Common Options

When executing the Dstat command, by default he collects data from the-CPU-、-disk-、-net-、-paging-、-system-, once a second. The default input dstat equals the input dstat -cdngy 1 ordstat -a 1。

1 CPU Status: CPU usage. The more interesting part of the report is the display of the user, the system, and the idle part, which better analyzes the current state of CPU usage. If you see the "Wait" column, the state of the CPU is a high usage value, which indicates that there are some other problems with the system. When the state of the CPU is "waits", that is because it is waiting for i/   Network statistics: Data sent and received by the network device, this column shows the total number of network receipts and sends data. 4 Paging Statistics: Paging activity of the system. Paging refers to a memory management technique used to find a system scenario, a large paging indicates that the system is using a large amount of swap space, or that the memory is very fragmented, and in most cases you want to see the value of page in (swapped in) and page out (swapped out) to 0 0. 5 System Statistics: This item shows interrupts (int) and Context switches (CSW). This statistic only makes sense when there are baseline comparisons. The higher statistics in this column usually indicate that a large number of processes are causing congestion and need to focus on the CPU. Your server will normally run some programs, so this always shows some values. 

By default, Dstat refreshes the data every second. Dstat can control the reporting interval and the number of reports by passing 2 parameters to run.

Dstat output default monitoring, report output time interval is 3 seconds, and the report output 10 results:

There are many parameters available in the Dstat command, which you can view with the Man Dstat command, and most of the commonly used parameters are:

1-L: Display load statistics 2-m: Display memory utilization (including Used,buffer,cache,free values) 3-r: Display i/o statistics 4-s: Display swap partition usage 5-–tcp: show commonly used TC P Statistic 10–udp: Displays some dynamic data for the monitored UDP interface and its current usage    

Dstat comes with a number of plugins that extend its functionality to a great extent. You can view some of their usage by looking at the/usr/share/dstat directory, which are commonly used:

1-–disk-util: Displays the busy state of a time Disk 2-–freespace: Displays the current disk space usage 3-–proc-count: Shows the number of programs running 4-–top-bio: indicates block i/  o Maximum process 5-–top-CPU: Graphical display of CPU-intensive processes 6-–top-io: show normal i/o max process 7-–top-mem: Show processes that occupy the most memory    
5, the use of Dstat

Dstat's strength is not just because it aggregates the monitoring results of multiple tools, but also because it enables advanced features through the accompanying plugins, such as identifying the most resource-intensive processes and users.

1. See who is occupying all the memory
Dstat-g-l-m-S--top-mem

The results are as follows

  

2. Show some data about CPU resource loss:
Dstat-c-y-l--proc-count--top-cpu

The results are as follows:

  

3. View the highest process information currently occupying I/O, CPU, memory, etc.
Dstat--top-mem--top-io--top-cpu

The results are as follows:

  

4. Output the results to a CSV file and output to the desktop
Dstat--output Dstat_output.csv
Second, interactive monitoring tools glances 1, Glances introduction

Glances is a psutil-based cross-platform system monitoring tool developed using the Python language. In all Linux command-line tools, it is most similar to the top command, which is the command-line interactive monitoring tool. However, glances implements a more complete monitoring than the top command, providing richer functionality.

Glances can display important system information in real-time on the user terminal and dynamically refresh the content. Glances is refreshed every 3 seconds, we can also use command line parameters to modify the frequency of refreshes. As with Dstat, glances can save the captured data to a file, but the difference is that glances provides an API interface for the application to fetch data from glances.

2. System information provided by the GLA NCEs
Kernel statistics and run queue information, 4 disk I/O speed, transfer and read/   disk adapters, 7 network I/O speed, transfer and read/ consume most resources of the process, 10 computer information and system resources. 
3, installation glances
Pip Install glances
4, the use of glances

The glances is very simple to use, and the direct input glances command enters an interactive interface similar to the top command. In this interface, more comprehensive, more readable information is displayed than top.

To increase readability, the glances will indicate different states in different colors. Among them, the green indicates good performance, the yuan should do any extra work; Blue indicates that there are some small problems in system performance, users should start to pay attention to system performance, purple indicates performance alarm, should take measures; red indicates a serious performance problem and should be dealt with immediately.
Glances is an interactive tool. Therefore, we can also enter commands to control the behavior of glances.

1. Common commands in Glances are:
M: Sort the system process according to memory usage; 5 I: Follow i/f: Show or hide file system information, 9 1: Separate display of each CPU usage. 
2. If we install the Bottle Web framework, we can also display the same monitoring interface as the command line terminal via a Web browser.
Pip Install bottleglance-w

Glances also supports the transfer of collected data to other service centers, including influxdb,cassandra,couchdb,opentsdb,prometheus,statsd,elasticsearch,rabbitmq/ Activemq,zeromq,kafka and Riemann.

Monitoring Linux systems with Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.