从磁盘使用统计中排除部分文件示例:
[root@foundation5 ~]# du -h --exclude "*.rpm" Downloads/
1.8M Downloads/
[root@foundation5 ~]# du -h Downloads/
2.9M Downloads/
找出指定目录中最大的 10 个文件
[root@foundation5 Pictures]# find -type f -exec du -k {} \; |sort -nrk 1 |head -n 3
一、编写saltstack的python扩展模块(以df -h为例)
1.创建模块目录
[root@rhel65-lockey1 ~]mkdir /srv/salt/_modules
2.创建模块文件
[root@rhel65-lockey1 _modules]# cat getDisk.py
#!/bin/usr/env python
def df():
cmd = 'df -h'
return __salt__['cmd.run'](cmd)
[root@rhel65-lockey1 _modules]# pwd
/srv/salt/_modules
3.同步模块(用主机rhel65-lockey5做实验)
[root@rhel65-lockey1 salt]# salt rhel65-lockey5 saltutil.sync_modules
4.rhel65-lockey5端查看改变
[root@rhel65-lockey5 ~]# cd /var/cache/salt/minion && tree
.
├── accumulator
├── extmods
│ ├── grains
│ │ ├── my_grains.py
│ │ └── my_grains.pyc
│ └── modules
│ └── getDisk.py##############
├── files
│ └── base
│ ├── _grains
│ │ └── my_grains.py
│ ├── haproxy
│ │ ├── files
│ │ │ ├── haproxy
│ │ │ ├── haproxy-1.6.11.tar.gz
│ │ │ └── haproxy.cfg
│ │ ├── install.sls
│ │ └── service.sls
│ ├── _modules
│ │ └── getDisk.py##############
。。。
5.模块调用(master端)
[root@rhel65-lockey1 salt]# salt rhel65-lockey5 getDisk.df
rhel65-lockey5:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 2.1G 15G 13% /
tmpfs 246M 32K 246M 1% /dev/shm
/dev/sda1 485M 34M 426M 8% /boot
二、将数据存储到mysql服务器
master端执行命令,minion端将结果存入远程数据库并发送一份到master端
1. 在minion端安装MySQL-python 以及 mysql客户端
[root@rhel65-lockey1 salt]# salt rhel65-lockey5 cmd.run "yum install MySQL-python mysql -y"
2. 设置数据存储(minion端)
[root@rhel65-lockey5 minion]# vim /etc/salt/minion#添加以下内容
mysql.host: '172.25.5.91'
mysql.user: 'salt'
mysql.pass: 'lockey23'
mysql.db: 'salt'
mysql.port: 3306
[root@rhel65-lockey5 minion]# /etc/init.d/salt-minion restart
3. 在另外一台主机安装mysql-server
[root@rhel65-lockey1 salt]# yum install mysql-server -y
[root@rhel65-lockey1 salt]# /etc/init.d/mysqld start
[root@rhel65-lockey1 salt]# mysql_secure_installation#安全设置
建表授权
CREATE DATABASE `salt`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
USE `salt`;
--
-- Table structure for table `jids`
--
DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
`jid` varchar(255) NOT NULL,
`load` mediumtext NOT NULL,
UNIQUE KEY `jid` (`jid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX jid ON jids(jid) USING BTREE;
--
-- Table structure for table `salt_returns`
--
DROP TABLE IF EXISTS `salt_returns`;
CREATE TABLE `salt_returns` (
`fun` varchar(50) NOT NULL,
`jid` varchar(255) NOT NULL,
`return` mediumtext NOT NULL,
`id` varchar(255) NOT NULL,
`success` varchar(10) NOT NULL,
`full_ret` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
KEY `id` (`id`),
KEY `jid` (`jid`),
KEY `fun` (`fun`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Table structure for table `salt_events`
--
DROP TABLE IF EXISTS `salt_events`;
CREATE TABLE `salt_events` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`tag` varc