自定义zabbix-agent rpm包

生产环境安装zabbix-agent可以考虑使用ansible进行批量推送,今天测试了一下rpm包自定义的方式,使用也比较方便。

1.安装软件包
yum install rpm-build rpmrebuild

2.解包
2.1 创建打包目录
[root@jk01 data]# rpmrebuild -p zabbix-agent-4.4.0-1.el7.x86_64.rpm
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
result: /data/rpmbuild/RPMS/x86_64/zabbix-agent-4.4.0-1.el7.x86_64.rpm

目录说明:
.
├── BUILD
├── BUILDROOT
├── RPMS
│   └── x86_64
│       └── zabbix-agent-4.4.0-1.el7.x86_64.rpm
├── SOURCES
├── SPECS
└── SRPMS

BUILDROOT    #编译后生成的软件临时安装目录
SPECS    #存放SPEC文件的目录
BUILD    #编译rpm包的临时目录
RPM    #最终生成的可安装rpm包的所在目录
SRPM    #软件最终的rpm源码格式存放路径

2.2 提取SPEC文件
[root@jk01 data]# rpmrebuild -s SPECS/zabbix-agent.spec -p zabbix-agent-4.4.0-1.el7.x86_64.rpm
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
警告:/data/zabbix-agent-4.4.0-1.el7.x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY

2.3 提取rpm文件
创建编译文件夹:
mkdir -p rpmbuild/BUILDROOT/zabbix-agent-4.4.0-1.el7.x86_64

提取rpm
[root@jk01 data]# cd rpmbuild/BUILDROOT/zabbix-agent-4.4.0-1.el7.x86_64/
[root@jk01 zabbix-agent-4.4.0-1.el7.x86_64]# rpm2cpio /data/zabbix-agent-4.4.0-1.el7.x86_64.rpm |cpio -div

[root@jk01 rpmbuild]# tree -F
.
|-- BUILD/
|-- BUILDROOT/
|   `-- zabbix-agent-4.4.0-1.el7.x86_64/
|       |-- etc/
|       |   |-- logrotate.d/
|       |   |   `-- zabbix-agent
|       |   `-- zabbix/
|       |       |-- zabbix_agentd.conf
|       |       `-- zabbix_agentd.d/
|       |           `-- userparameter_mysql.conf
|       |-- usr/
|       |   |-- lib/
|       |   |   |-- systemd/
|       |   |   |   `-- system/
|       |   |   |       `-- zabbix-agent.service
|       |   |   `-- tmpfiles.d/
|       |   |       `-- zabbix-agent.conf
|       |   |-- sbin/
|       |   |   `-- zabbix_agentd*
|       |   `-- share/
|       |       |-- doc/
|       |       |   `-- zabbix-agent-4.4.0/
|       |       |       |-- AUTHORS
|       |       |       |-- COPYING
|       |       |       |-- ChangeLog
|       |       |       |-- NEWS
|       |       |       `-- README
|       |       `-- man/
|       |           `-- man8/
|       |               `-- zabbix_agentd.8.gz
|       `-- var/
|           |-- log/
|           |   `-- zabbix/
|           `-- run/
|               `-- zabbix/
|-- RPMS/
|   `-- x86_64/
|       `-- zabbix-agent-4.4.0-1.el7.x86_64.rpm
|-- SOURCES/
|-- SPECS/
|   `-- zabbix-agent.spec
`-- SRPMS/

2.4自定义zabbix_agentd.conf
[root@jk01 data]# grep -v ^# /data/rpmbuild/BUILDROOT/zabbix-agent-4.4.0-1.el7.x86_64/etc/zabbix_agentd.conf  |sed '/^$/d'
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=zabbix.hunt1574.com
ListenPort=10050
ServerActive=zabbix.hunt1574.com
Hostname=Zabbix server
AllowRoot=1
Include=/etc/zabbix/zabbix_agentd.d/*.conf

2.5修改spec文件
增加自定义脚本的路径及文件权限,同时使用替换功能将安装目标的ip写入配置文件。
[root@jk01 SPECS]# cat zabbix-agent.spec
%files
%dir %attr(0755, root, root) "/etc/zabbix/scripts"
%config(noreplace) %attr(0755, root, root) "/etc/zabbix/scripts/tcp-port.sh"
%config(noreplace) %attr(0644, root, root) "/etc/zabbix/zabbix_agentd.d/userparameter_tcp_port.conf"
...

%pre -p /bin/sh
getent group zabbix > /dev/null || groupadd -r zabbix
getent passwd zabbix > /dev/null || \
        useradd -r -g zabbix -d /var/lib/zabbix -s /sbin/nologin \
        -c "Zabbix Monitoring System" zabbix
grep "zabbix" >/dev/null || echo "zabbix ALL=(ALL) NOPASSWD: ALL,!/usr/bin/su root,!/usr/bin/su - root,!/usr/bin/passwd,!/usr/bin/yum remove * ,!/usr/bin/yum install iptables-services,!/bin/systemctl stop *,!/bin/systemctl start *,!/bin/systemctl restart *,!/bin/sys" >>/etc/sudoers
:
%post -p /bin/sh

if [ $1 -eq 1 ] ; then
        # Initial installation
        /usr/bin/systemctl preset zabbix-agent.service >/dev/null 2>&1 || :
        ip=`hostname -I|awk '{print $1}'|sed 's/ //g'`>/dev/null
        sed -i "s#Hostname=Zabbix server#Hostname=$ip#g" /etc/zabbix/zabbix_agentd.conf >/dev/null
fi

3.打包
rpmbuild常用参数:
-bb 只编译二进制rpm包
-bs 只编译源码rpm包
-ba 同时编译二进制和源码rpm包
[root@jk01 SPECS]# rpmbuild -ba zabbix-agent.spec
处理文件:zabbix-agent-4.4.0-1.el7.x86_64
Provides: config(zabbix-agent) = 4.4.0-1.el7 zabbix-agent = 4.4.0-1.el7 zabbix-agent(x86-64) = 4.4.0-1.el7
Requires(interp): /bin/sh /bin/sh /bin/sh /bin/sh
Requires(rpmlib): rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rpmlib(CompressedFileNames) <= 3.0.4-1
Requires(pre): /bin/sh
Requires(post): /bin/sh
Requires(preun): /bin/sh
Requires(postun): /bin/sh
Obsoletes: zabbix
写道:/data/rpmbuild/SRPMS/zabbix-agent-4.4.0-1.el7.src.rpm
写道:/data/rpmbuild/RPMS/x86_64/zabbix-agent-4.4.0-1.el7.x86_64.rpm
执行(%clean): /bin/sh -e /var/tmp/rpm-tmp.OMkyQ8
+ umask 022
+ cd /data/rpmbuild/BUILD
+ /usr/bin/rm -rf /data/rpmbuild/BUILDROOT/zabbix-agent-4.4.0-1.el7.x86_64
+ exit 0

4.加入到yum源
创建yum目录
#mkdir -p /var/www/html/yum/zabbix/4.4/rhel-inter/7/

复制文件至yum目录
[root@jk01 RPMS]# scp -r x86_64 172.10.10.200:/var/www/html/yum/zabbix/4.4/rhel-inter/7/

创建repodata
[root@yum x86_64]# createrepo /var/www/html/yum/zabbix/4.4/rhel-inter/7/x86_64
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 0 pkgs
Spawning worker 2 with 0 pkgs
Spawning worker 3 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

5.测试安装
# cat /etc/yum.repos.d/zabbix.repo
[zabbix-4.4]
name=zabbix-4.4
baseurl=http://172.10.10.200/yum/zabbix/4.4/rhel-inter/7/x86_64
gpgcheck=0
enabled=1

# yum install zabbix-agent
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
zabbix-4.4                                                                                                | 2.9 kB  00:00:00
zabbix-4.4/primary_db                                                                                     | 2.7 kB  00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 zabbix-agent.x86_64.0.4.4.0-1.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=================================================================================================================================
 Package                         架构                      版本                              源                             大小
=================================================================================================================================
正在安装:
 zabbix-agent                    x86_64                    4.4.0-1.el7                       zabbix-4.4                    419 k

事务概要
=================================================================================================================================
安装  1 软件包

总下载量:419 k
安装大小:1.7 M
Is this ok [y/d/N]: y
Downloading packages:
zabbix-agent-4.4.0-1.el7.x86_64.rpm                                                                       | 419 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : zabbix-agent-4.4.0-1.el7.x86_64                                                                              1/1
  验证中      : zabbix-agent-4.4.0-1.el7.x86_64                                                                              1/1

已安装:
  zabbix-agent.x86_64 0:4.4.0-1.el7

完毕!

参考:https://blog.csdn.net/itas109/article/details/104226935


上一篇:centos7.5记录一次yum包的过程


下一篇:Centos7下卸载彻底MySQL数据库