Brief introduction
Func provides a very rich function module, including commandmodule (Execute command), copyfilemodule (copy file), Cpumodule (CPU information), Diskmodule (disk information), Filetrackermodule (file tracking), Iptablesmodule (iptables Management), Mountmodule (Mount Mount), Nagiosservermodule (Nagios management), Networktest (network test), Processmodule (Process Management), Sysctlmodule (sysctl Management), Snmpmodule (SNMP information), and so on.
Command invocation format:
Func < target host > Call <module_name (module name)> <method_name (method name)> <module_ Args (module parameters)>
Cases:
' Test ' ' df-h ' # Get test host disk information
Results:
('wx', [0,'939M 16K 939M 1%/dev/shm\n/dev/sda1 477M 103M 349M 23%/boot\n','])
Note: The module command execution results are returned as a Python tuple string, and the API call is returned as a dictionary
Api:
# !/usr/bin/env python # -*-coding:utf-8-*- Import = func. Client ('test'#test is the target host and will operate on all target hosts using ' * 'result = Client.command.run ('free-m'# All module calls just need to be added here to print result
Select target Host
Func Select target host operation object Support ' * ' and '? ' Method matches, where * represents any number of characters,? Represents a single arbitrary character. Multiple target hosts separated by semicolons (;)
Cases:
' sn2013-*-02? ' ' Uptime ' # sn2013-*-02? can match sn2013-08-021, sn2013-09-022, etc.
Frequently used modules detailedfunc Command function parameter example
View all host uptime, turn on 5 threads to run asynchronously with a time-out of 3 seconds
' * ' call--forks='5'uptime'
Format output, default format for Python tuples, add--json or--xml to output JSON and XML formats
' * ' call--forks='5'uptime'
Execute Command Module
Function:
Commandmodule implementation of Linux remote command call execution
Command:
' * ' ' ulimit-a ' 'test' 'free-m '
Api:
Client.command.run ('free-m')
File copy function
Function:
Copyfilemodule enables the master side to copy files to the target host, similar to the SCP function
Command:
# format: ' * ' copyfile-f local file--remotepath remote file path # Example:'*' copyfile-f/etc/sysctl.conf--remotepath/tmp/sysctl.conf
Api:
Client.local.copyfile.send ('/etc/sysctl.conf','/tmp/sysctl.conf ')
CPU Information Module
Function:
The cpumodule obtains the remote host CPU information and supports the averaging by time (in seconds) sampling.
Command:
' * ' '*'# take 10 seconds to average
Api:
Client.cpu.usage (10)
Disk Information Module
Function:
Diskmodule implementation gets the disk partition information for the remote host, with the parameter partition label, such as the/data partition
Command:
' * ' '*' call disk Usage/data
Api:
Client.disk.usage ('/dev/sda3')
Copy remote file module
Function:
Getfilemodule implementation pull the remote Linux host to specify files to the master side directory, does not support command-line mode
Api:
Client.getfile.get ('/etc/sysctl.conf','/tmp/')
iptables Management Module
Function:
Iptablesmodule implementing remote host Iptables configuration
Command:
' * ' # Discard packets sent from the local 53 port to the remote 192.168.0.0/24 network segment ' * ' # Discard packets received from 192.168.0.10
Api:
Client.iptables.port.drop_to (8080,'192.168.0.10','tcp', ' DST ')
System Hardware Information Module
Function:
Hardwaremodule returning remote host system hardware information
Command:
' * ' '*' call hardware Hal_info
Api:
Client.hardware.info (with_devices=True) client.hardware.hal_info ()
System Mount Management module
Function:
Mountmodule implementation of remote host Linux system mount, offload partition management
Command:
' * ' # get mount information for a remote host ' * ' # remote host mounts the/DEV/SDA3 device to the/data directory ' * ' ' /data ' # uninstalling the/data directory
Api:
client.mount.list () client.mount.umount('/data') Client.mount.mount ( '/dev/sda3','/data')
System Process Management module
Function:
Processmodule implementing remote Linux Host Process management
Command:
' * ' ' aux ' # get process information, equivalent to PS aux ' * ' call process pkill nginx-9'*' call process kill Nginx SIGHUP
Api:
Client.process.info ('aux') client.process.pkill ('nginx ','-9') client.process.kill ('nginx') ,'SIGHUP')
system service Management module
Function:
Servicemodule implementation of remote Linux host system service management
Command:
' * ' # start Nginx
Api:
Client.service.start ('nginx')
system kernel parameter management module
Function:
Sysctlmodule realization of kernel parameter management of remote Linux host system
Command:
' * ' # get all kernel parameters ' * ' # Get kernel parameters Net.nf_conntrack_max ' * ' # Set kernel parameters Net.nf_conntrack_max
Api:
client.sysctl.list () client.sysctl.get ('net.ipv4.icmp_echo_ignore_broadcasts') ) client.sysctl.set ('net.ipv4.tcp_syncookies', 1)
Resources:
Organized according to Liu Tians, "python automated operation and maintenance technology and best practices"
Func Series 2: Common modules and APIs