Examples of ansible disk usage filtering

Source: Internet
Author: User
Tags json disk usage python script

At the end of the year although there are a variety of alarms, but still need to do a full network patrol, here to disk utilization rate as an example. Because the current platform is using the commercial version of the BMC, the Automation tool in the implementation of the distribution are very not, but do not do well in the return of the value--of course, and it is the commercialization of tools, a lot of things do not open source, with the shell of the concept of grammar to write NSH when the implementation does not pass, Nnd always ask the original support. Just in the test environment has previously installed ansible environment, also tested under the ansible to achieve this function.

I. Achievement of the goal

Obtain a host with a disk greater than 75% for all hosts and output to the following format:

Host IP host name disk mount point information disk usage
If there are more than 75% partitions on the host1, write more than one. When no one on the host is compliant, nothing is exported.

Note: The current network host has two Bond network cards, one of which is configured with 10 IP, and the other is 192 IP. Here is a 10-segment IP.

Second, ansible + awk simple output

The result of my handling of this problem is implemented using a Python script written by the Ansible API. Take a look at the results of my discussion in a technical group and a Daniel (click on the picture to see the larger image).

As pictured above, Daniel's approach is handled by the awk next method, and the next function of awk is recorded, as in the awk next multiline merge.

Here are a few points of attention to note:

1, Daniel's writing in the screening of the not rigorous, the $ should have an int transformation, but not the right to take the value

[Root@361way ~]# Df-hp|awk ' nr>1 && $ > 20 '
/DEV/XVDA1 20G 4.6G 15G 25%/
/dev/xvdb 20G 645M 18G 4%/data1
[Root@361way ~]# Df-hp|awk ' nr>1 && int ($) > 20 '
/DEV/XVDA1 20G 4.6G 15G 25%/
Did you see that if the result of the int format conversion is the information of all partitions, and the int gets the result we want?

2, in the ansible implementation of the $ before the addition of escape

# ansible all-m shell-a "Df-hp|awk ' nr>1 && int ($) > 50 '"
10.212.52.252 | FAILED | rc=2 >>
Awk:fatal:0 is invalid as number of arguments for int
[Root@361way ~]# Df-hp|awk ' nr>1 && int (\$5) > 20 '
awk:nr>1 && Int (\$5) > 20
awk: ^ backslash not last character in line
As the result above, if the ansible is executed without escaping, there will be an error prompt, if the addition of escape on the host side will automatically be removed from the escape character results. When executing on the host side, an error is also made if the escape is added. So the host side must not be added escape.

3, in the actual application, the output may have a gap with the output of Daniel

[root@localhost ~]# ansible all-m shell-a ' Df-hp|awk ' nr>1 && int (\$5) > ' "|awk '/success/{ip=$1;nex T}{print ip,$0} '
10.212.52.252/dev/sda9       9.9g  2.9G  6.5G  31% /OPT
10.212.52.252/dev/sda6       5.0g  1.9g  2.8G  41%/tmp
10.212.52.252/dev/sda5       9.9g  3.9g  5.5G  42%/usr
10.212.52.252
10.212.52.14/dev/cciss/c0d0p5  9.9g  3.2g  6.3g  34%/usr
10.212.52.14
10.212.52.16/dev/cciss/c0d0p7  9.9g  4.0g  5.4g  43%/tmp
10.212.52.16/DEV/CCISS/C0D0P5   9.9g  2.9g  6.5g  31%/usrdf: '/root/.gvfs ': Permission denied
10.212.52.16
The result above is I'm in my own test loop The results of the implementation of the border. You can see that the extra blank line also prints the host's IP. It will also be noted that I am here ansible output of the success is lowercase.

Do not know the above problem is not the use of environmental differences caused. The environment I run is ansible host for redhat6,ansible version 1.9, the data host has REDHAT6 and SUSE11. However, this is a small problem and can also be processed to get the normal result.

4, Ansible API implementation

The result of Daniel above, I use the Ansible API to execute, as below, can compare:

[Root@localhost ~]# cat/tmp/test.py
#!/usr/bin/env python
# Coding=utf-8
# author:www.111cn.net
# mail:itybku@139.com
Import Ansible.runner
#import JSON
Runner = Ansible.runner.Runner (
Module_name= ' Shell ',
module_args= "Df-hp|awk ' nr>1 && Int ($) >30 '",
Pattern= ' All ',
forks=10
)
Results = Runner.run ()
#print Results
for [hostname, result] in results[' contacted '].items ():
If not ' failed ' in result:
For line in result[' stdout '].split (' \ n '):
Print '%s%s '% (hostname, line)
# Implementation results are as follows
[Root@localhost ~]# python/tmp/test.py
10.212.52.16/DEV/CCISS/C0D0P7 9.9G 4.0G 5.4G 43%/tmp
10.212.52.16/DEV/CCISS/C0D0P5 9.9G 2.9G 6.5G 31%/usr
10.212.52.252/DEV/SDA9 9.9G 2.9G 6.5G 31%/opt
10.212.52.252/dev/sda6 5.0G 1.9G 2.8G 41%/tmp
10.212.52.252/dev/sda5 9.9G 3.9G 5.5G 42%/usr
10.212.52.14/DEV/CCISS/C0D0P5 9.9G 3.2G 6.3G 34%/usr
Third, or the Ansible API to get disk information

The actual execution of the above method is less than the effect we expected to require a host name. Here I do the execution of the script implementation, the implementation of the results are as follows:

#/bin/bash
# author:www.111cn.net
ip= ' IP add show|grep inet|grep 10|awk ' {print $} '
Df-hl|grep ' ^/' |se d ' s/%//g ' |awk ' {if ($5>30) print $} ' |while read line
do
    echo $IP ' hostname ' $line
done< br> # Execution results are as follows
# sh aa.sh
10.212.52.253/24 localhost/dev/sda3 9.5G 5.7G 3.4G/
10.212.52.253/24 localhost/d Ev/sda2 39G 19G 18G 52/home
10.212.52.253/24 localhost/dev/sda6 9.5G 7.1G 2.0G 78/usr
The results of executing the script using the Ansible API are as follows:

# python dfscript.py
DF: '/ROOT/.GVFS ': Permission denied
DF: '/ROOT/.GVFS ': Permission denied
10.212.52.16/24 linux/dev/cciss/c0d0p7 9.9G 4.0G 5.4G 43/tmp
10.212.52.16/24 linux/dev/cciss/c0d0p5 9.9G 2.9G 6.5G 31/usr
10.212.52.252/24 zjhz-bmc-test/dev/sda9 9.9G 2.9G 6.5G 31/opt
10.212.52.252/24 zjhz-bmc-test/dev/sda6 5.0G 1.9G 2.8G 41/tmp
10.212.52.252/24 zjhz-bmc-test/dev/sda5 9.9G 3.9G 5.5G 42/usr
10.212.52.14/24 linux/dev/cciss/c0d0p5 9.9G 3.2G 6.3G 34/usr
dfscript.py script contents are as follows

# Cat dfscript.py
#!/usr/bin/env python
# Coding=utf-8
# author:www.111cn.net
# mail:itybku@139.com
Import Ansible.runner
#import JSON
Runner = Ansible.runner.Runner (
Module_name= ' script ',
Module_args= "Aa.sh",
Pattern= ' All ',
forks=10
)
Results = Runner.run ()
#print Results
for [hostname, result] in results[' contacted '].items ():
If not ' failed ' in result:
For line in result[' stdout '].split (' \ r \ n '):
#print '%s%s '% (hostname, line)
Print Line
When grep standard output is directly performed on the results of the script, it is found that the lines and rows are branch by way of \ r \ n.

So in terms of data acquisition, try to get in the way of Ansible API, and the use of API is very simple, can not be the replacement of several parameters after the call to the Run method, and ultimately processing the results. When it comes to multiple information acquisition, it is recommended that you use a custom module method to retrieve the required data and return the format required by the---ansible custom module in JSON. The returned data can then be processed by the API or otherwise.

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.