两种姿势教你如何定制RPM包(rpmbuild与fpm)

栏目: 服务器 · 发布时间: 5年前

内容简介:一、rpmbuild方式定制rpm包rpmbuid是用于制作rpm格式包的工具。rpm 4.4.x版本之前,rpmbuid工具默认的工作车间为/usr/src/redhat,所以造成普通用户不能制作rpm包。rpm 4.5.x版本开始,将rpmbuid工具默认的工作车间为$HOME/rpmbuild(用户家目录),并且推荐用户尽量不用root账号制作rpm包。rpmbuild默认工作车间为/usr/lib/rpm/macros这个文件,具体由%_topdir宏变量进行定义。官方不建议在/usr/lib/rp

一、rpmbuild方式定制rpm包

rpmbuid是用于制作rpm格式包的工具。rpm 4.4.x版本之前,rpmbuid工具默认的工作车间为/usr/src/redhat,所以造成普通用户不能制作rpm包。rpm 4.5.x版本开始,将rpmbuid工具默认的工作车间为$HOME/rpmbuild(用户家目录),并且推荐用户尽量不用root账号制作rpm包。

#rpm版本查看方法:
[root@localhost ~]# rpm -qa | grep rpm
perl-srpm-macros-1-8.el7.noarch
rpm-python-4.11.3-35.el7.x86_64
rpm-4.11.3-35.el7.x86_64
rpm-build-libs-4.11.3-35.el7.x86_64
redhat-rpm-config-9.1.0-87.el7.centos.noarch
rpm-build-4.11.3-35.el7.x86_64
rpm-libs-4.11.3-35.el7.x86_64

rpmbuild默认工作车间为/usr/lib/rpm/macros这个文件,具体由%_topdir宏变量进行定义。官方不建议在/usr/lib/rpm/macros目录中更改这个工作路径。如果需要则可以在用户家目录下建立一个.rpmmacros隐藏文件,在里面重新定义%_topdir,指向一个新的目录名。内容一般为:

[gxm@localhost ~]$ cat .rpmmacros
%_topdir    $HOME/新目录

在%_topdir目录下一般需要有6个目录(实际操作的时候执行命令自动创建):
备注:执行rpmdev-setuptree命令会在当前用户的家目录下的rpmbuild目录(如果该目录不存在也会被自动创建)里自动建立上述目录。

目录名

macros宏名

说明

BUILD

%_builddir

编译rpm包的临时目录

BUILDROOT

%_buildrootdir

编译后生成的软件临时安装目录

RPMS

%_rpmdir

最终生成的可安装rpm包的所在目录

SOURCES

%_sourcedir

所有源代码和补丁文件的存放目录

SPECS

%_specdir

存放SPEC文件的目录(重要)

SRPMS

%_srcrpmdir

软件最终的rpm源码格式存放路径(暂时忽略掉,别挂在心上)  

当上述目录建立好之后,将所有用于生成rpm包的源代码、 shell 脚本、配置文件都拷贝到SOURCES目录里,注意通常情况下源码的压缩格式都为*.tar.gz格式。然后将编辑好的SPEC文件,命名为“软件名-版本.spec”,将其拷贝到SPECS目录下。
最后切换SPEC目录下执行命令制作rpm包:

[gxm@localhost ~]$ cd SPEC
[gxm@localhost SPEC]$ rpmbuild -bb 软件名-版本.spec
#打包好的rpm包在RPMS目录下

SPEC文件是最核心的,在它里面定义了头部信息和一些“阶段”(%prep、%build、%install、%clean、%pre、%post、%preun和%postun),当rpmbuild执行时它首先会去解析SPEC文件,然后依次执行每个“阶段”里的指令。

参考:

http://blog.chinaunix.net/uid-23069658-id-3944462.html

1、安装相关包和查看默认工作车间

[root@localhost ~]# yum -y install rpm-build rpmdevtools
[root@localhost ~]# rpmbuild --showrc | grep topdir
-14: _builddir    %{_topdir}/BUILD
-14: _buildrootdir    %{_topdir}/BUILDROOT
-14: _rpmdir    %{_topdir}/RPMS
-14: _sourcedir    %{_topdir}/SOURCES
-14: _specdir    %{_topdir}/SPECS
-14: _srcrpmdir    %{_topdir}/SRPMS
-14: _topdir    %{getenv:HOME}/rpmbuild

2、新增普通用户

[root@localhost ~]# useradd gxm
[root@localhost ~]# passwd gxm
更改用户 gxm 的密码 。
新的 密码:
无效的密码: 密码未通过字典检查 - 它基于字典单词
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost ~]# su - gxm
[gxm@localhost ~]$

3、自动生成rpm打包所需目录

[gxm@localhost ~]$ rpmdev-setuptree
[gxm@localhost ~]$ ll
总用量 0
drwxrwxr-x 7 gxm gxm 67 4月  16 11:38 rpmbuild
[gxm@localhost ~]$ cd rpmbuild/
[gxm@localhost rpmbuild]$ ll
总用量 0
drwxrwxr-x 2 gxm gxm 6 4月  16 11:38 BUILD
drwxrwxr-x 2 gxm gxm 6 4月  16 11:38 RPMS
drwxrwxr-x 2 gxm gxm 6 4月  16 11:38 SOURCES
drwxrwxr-x 2 gxm gxm 6 4月  16 11:38 SPECS
drwxrwxr-x 2 gxm gxm 6 4月  16 11:38 SRPMS

4、将源码、配置文件等放入SOURCES目录下

[gxm@localhost rpmbuild]$ cd SOURCES/
[gxm@localhost SOURCES]$ wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.4/zabbix-4.0.4.tar.gz
[gxm@localhost SOURCES]$ 上传其它文件

5、创建SPEC模板文件熟悉

[gxm@localhost rpmbuild]$ cd SPECS/
[gxm@localhost SPECS]$ rpmdev-newspec -o zabbix_agent-4.0.4.spec
zabbix_agent-4.0.4.spec created; type minimal, rpm version >= 4.11.
[gxm@localhost SPECS]$ cat zabbix_agent-4.0.4.spec
Name:           zabbix_agent-4.0.4
Version:        
Release:        1%{?dist}
Summary:        
License:        
URL:            

Source0:        
BuildRequires:  
Requires:       

%description

%prep

%setup -q
#这里要特别注意,如果压缩包和解压出来的文件名不相同,则用%setup -n 解压后目录名

%build

%configure

make %{?_smp_mflags}

%install

rm -rf $RPM_BUILD_ROOT

%make_install

%files

%doc

%changelog

SPEC完整版如下:

%define zabbix_user zabbix
%define zabbix_dir /home/zabbix
Name:    zabbix_agent
Version:    4.0.4       
Release:    1%{?dist}
Summary:    zabbix agent
Group:    zabbix
License:    GPL
URL:    www.zabbix.com
Source0:    zabbix-4.0.4.tar.gz
Source1:    zabbix_agentd
Source2:    discovertcpport.sh
Source3:    portlist.txt
Source4:    discoveryproc.sh
Source5:    proclist.txt
Source6:    cmdline-jmxclient-0.10.3.jar
Source7:    jvm.py
Source8:    tomcat.py
Source9:    ca.crt
Source10:    client.crt
Source11:    client.key
Source12:    java
Source13:    monitor_port
Source14:    monitor_proc

BuildRequires:    gcc, gcc-c++, pcre, pcre-devel, openssl, openssl-devel
Requires:    gcc, gcc-c++, pcre, pcre-devel, openssl, openssl-devel

%description
Zabbix agentd 4.0.4

%pre
grep %zabbix_user /etc/passwd > /dev/null
if [ $? != 0 ]
    then useradd %zabbix_user -M -s /sbin/nologin
fi
[ -d %{zabbix_dir} ]||rm -rf %{zabbix_dir}

%post
mkdir -p %{zabbix_dir}/logs
chown -R %zabbix_user.%zabbix_user %{zabbix_dir}
chmod -R 755 %{zabbix_dir}
sed -i "s/LogFile=\/tmp\/zabbix_agentd\.log/LogFile=%{zabbix_dir}/logs\/zabbix_agentd.log/g" %{zabbix_dir}/etc/zabbix_agentd.conf
sed -i "s/ServerActive=127\.0\.0\.1/ServerActive=192\.168\.7\.10/g" %{zabbix_dir}/etc/zabbix_agentd.conf
ipaddr=`ip a | grep -w "inet" | egrep "eno16777736|eth0" |sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g'`
sed -i "s/Hostname=Zabbix server/Hostname=$ipaddr/g" %{zabbix_dir}/etc/zabbix_agentd.conf
sed -i "s/Timeout=3/Timeout=30/g" %{zabbix_dir}/etc/zabbix_agentd.conf
echo "UnsafeUserParameters=1" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "EnableRemoteCommands=1" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "Include=%{zabbix_dir}/etc/zabbix_agentd.conf.d/" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "PidFile=%{zabbix_dir}/zabbix_agentd.pid" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "LogFileSize=0" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "StartAgents=0" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "HostMetadataItem=system.uname" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "User=zabbix" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "TLSConnect=cert" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "TLSAccept=cert" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "TLSCAFile=%{zabbix_dir}/zabbix_ssl/ca.crt" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "TLSCertFile=%{zabbix_dir}/zabbix_ssl/client.crt" >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo "TLSKeyFile=%{zabbix_dir}/zabbix_ssl/client.key" >>%{zabbix_dir}/etc/zabbix_agentd.conf
sed -i "s/BASEDIR=\/usr\/local/BASEDIR=\/home\/zabbix/g" /etc/rc.d/init.d/zabbix_agentd
systemctl daemon-reload
/etc/init.d/zabbix_agentd start
systemctl enable zabbix_agentd

%preun
systemctl stop zabbix_agentd

%postun
userdel -r zabbix
rm -rf /etc/rc.d/init.d/zabbix_agentd
rm -rf %{zabbix_dir}

%prep
%setup -n zabbix-4.0.4

%build
./configure --prefix=%{zabbix_dir} --enable-agent --with-openssl
make %{?_smp_mflags}

%install
rm -rf %{buildroot}
install -d %{buildroot}/etc/profile.d
make install DESTDIR=%{buildroot}
install -p -D -m 0755 %{SOURCE1}    %{buildroot}/etc/rc.d/init.d/zabbix_agentd
install -p -D %{SOURCE2}        %{buildroot}%{zabbix_dir}/script/monitor_port/discovertcpport.sh
install -p -D %{SOURCE3}        %{buildroot}%{zabbix_dir}/script/monitor_port/portlist.txt
install -p -D %{SOURCE4}        %{buildroot}%{zabbix_dir}/script/monitor_proc/discoveryproc.sh
install -p -D %{SOURCE5}        %{buildroot}%{zabbix_dir}/script/monitor_proc/proclist.txt
install -p -D %{SOURCE6}        %{buildroot}%{zabbix_dir}/script/jvm/cmdline-jmxclient-0.10.3.jar
install -p -D %{SOURCE7}        %{buildroot}%{zabbix_dir}/script/jvm/jvm.py
install -p -D %{SOURCE8}        %{buildroot}%{zabbix_dir}/script/jvm/tomcat.py
install -p -D %{SOURCE9}        %{buildroot}%{zabbix_dir}/zabbix_ssl/ca.crt
install -p -D %{SOURCE10}       %{buildroot}%{zabbix_dir}/zabbix_ssl/client.crt
install -p -D %{SOURCE11}       %{buildroot}%{zabbix_dir}/zabbix_ssl/client.key
install -p -D %{SOURCE12}       %{buildroot}%{zabbix_dir}/etc/zabbix_agentd.conf.d/java
install -p -D %{SOURCE13}       %{buildroot}%{zabbix_dir}/etc/zabbix_agentd.conf.d/monitor_port
install -p -D %{SOURCE14}       %{buildroot}%{zabbix_dir}/etc/zabbix_agentd.conf.d/monitor_proc
echo 'export PATH=%{zabbix_dir}/bin:%{zabbix_dir}/sbin:$PATH' > %{buildroot}/etc/profile.d/%{name}.sh

%files
%defattr (-,root,root,0755)
%{zabbix_dir}
%{zabbix_dir}/*
/etc/profile.d/%{name}.sh
%attr(0755,root,root) /etc/rc.d/init.d/zabbix_agentd

%changelog

%clean
rm -rf %{buildroot}

6、打包编译

[root@localhost ~]# yum install gcc, gcc-c++, pcre, pcre-devel, openssl, openssl-devel -y
[gxm@localhost SPECS]$ rpmbuild -ba zabbix_agent-4.0.4.spec
[gxm@localhost SPECS]$ rpmbuild -bb zabbix_agent-4.0.4.spec
#-ba和-bb二选一

7、下载安装

[gxm@localhost SPECS]$ sz ../RPMS/x86_64/zabbix_agent-4.0.4-1.el7.centos.x86_64.rpm
[root@localhost ~]# yum localinstall zabbix_agent-4.0.4-1.el7.centos.x86_64.rpm -y
[root@localhost ~]# rpm -qpi zabbix_agent-4.0.4-1.el7.centos.x86_64.rpm
Name        : zabbix_agent
Version     : 4.0.4
Release     : 1.el7.centos
Architecture: x86_64
Install Date: (not installed)
Group       : zabbix
Size        : 813091
License     : GPL
Signature   : (none)
Source RPM  : zabbix_agent-4.0.4-1.el7.centos.src.rpm
Build Date  : 2019年04月20日 星期六 22时22分05秒
Build Host  : localhost
Relocations : (not relocatable)
URL         : www.zabbix.com
Summary     : zabbix agent
Description :
Zabbix agentd 4.0.4

[root@localhost ~]# rpm -qpl zabbix_agent-4.0.4-1.el7.centos.x86_64.rpm
/etc/profile.d/zabbix_agent.sh
/etc/rc.d/init.d/zabbix_agentd
/home/zabbix
/home/zabbix/bin
/home/zabbix/bin/zabbix_get
/home/zabbix/bin/zabbix_sender
/home/zabbix/etc
/home/zabbix/etc/zabbix_agentd.conf
/home/zabbix/etc/zabbix_agentd.conf.d
/home/zabbix/etc/zabbix_agentd.conf.d/java
/home/zabbix/etc/zabbix_agentd.conf.d/monitor_port
/home/zabbix/etc/zabbix_agentd.conf.d/monitor_proc
/home/zabbix/lib
/home/zabbix/lib/modules
/home/zabbix/sbin
/home/zabbix/sbin/zabbix_agentd
/home/zabbix/script
/home/zabbix/script/jvm
/home/zabbix/script/monitor_port
/home/zabbix/script/monitor_proc
/home/zabbix/share
/home/zabbix/share/man
/home/zabbix/share/man/man1
/home/zabbix/share/man/man1/zabbix_get.1
/home/zabbix/share/man/man1/zabbix_sender.1
/home/zabbix/share/man/man8
/home/zabbix/share/man/man8/zabbix_agentd.8
/home/zabbix/zabbix_ssl

[root@localhost ~]# rpm -qp --scripts zabbix_agent-4.0.4-1.el7.centos.x86_64.rpm
preinstall scriptlet (using /bin/sh):
grep zabbix /etc/passwd > /dev/null
if [ $? != 0 ]
    then useradd zabbix -M -s /sbin/nologin
fi
[ -d /home/zabbix ]||rm -rf /home/zabbix
postinstall scriptlet (using /bin/sh):
mkdir -p /home/zabbix/logs
chown -R zabbix.zabbix /home/zabbix
chmod -R 755 /home/zabbix
sed -i "s/LogFile=\/tmp\/zabbix_agentd\.log/LogFile=/home/zabbix/logs\/zabbix_agentd.log/g" /home/zabbix/etc/zabbix_agentd.conf
sed -i "s/ServerActive=127\.0\.0\.1/ServerActive=192\.168\.7\.10/g" /home/zabbix/etc/zabbix_agentd.conf
ipaddr=`ip a | grep -w "inet" | egrep "eno16777736|eth0" |sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g'`
sed -i "s/Hostname=Zabbix server/Hostname=$ipaddr/g" /home/zabbix/etc/zabbix_agentd.conf
sed -i "s/Timeout=3/Timeout=30/g" /home/zabbix/etc/zabbix_agentd.conf
echo "UnsafeUserParameters=1" >>/home/zabbix/etc/zabbix_agentd.conf
echo "EnableRemoteCommands=1" >>/home/zabbix/etc/zabbix_agentd.conf
echo "Include=/home/zabbix/etc/zabbix_agentd.conf.d/" >>/home/zabbix/etc/zabbix_agentd.conf
echo "PidFile=/home/zabbix/zabbix_agentd.pid" >>/home/zabbix/etc/zabbix_agentd.conf
echo "LogFileSize=0" >>/home/zabbix/etc/zabbix_agentd.conf
echo "StartAgents=0" >>/home/zabbix/etc/zabbix_agentd.conf
echo "HostMetadataItem=system.uname" >>/home/zabbix/etc/zabbix_agentd.conf
echo "User=zabbix" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSConnect=cert" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSAccept=cert" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSCAFile=/home/zabbix/zabbix_ssl/ca.crt" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSCertFile=/home/zabbix/zabbix_ssl/client.crt" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSKeyFile=/home/zabbix/zabbix_ssl/client.key" >>/home/zabbix/etc/zabbix_agentd.conf
sed -i "s/BASEDIR=\/usr\/local/BASEDIR=\/home\/zabbix/g" /etc/rc.d/init.d/zabbix_agentd
systemctl daemon-reload
/etc/init.d/zabbix_agentd start
systemctl enable zabbix_agentd
preuninstall scriptlet (using /bin/sh):
systemctl stop zabbix_agentd
postuninstall scriptlet (using /bin/sh):
userdel -r zabbix
rm -rf /etc/rc.d/init.d/zabbix_agentd
rm -rf /home/zabbix

二、fpm方式定制rpm包

fpm工具其实是对rpmbuild进行封装,目的是使打包变成容易。由于此 工具ruby 语言编写的,所以 系统需要安装ruby,且ruby版本号大于1.8.5。

fpm开源项目:

https://github.com/jordansissel/fpm

1、安装ruby环境和gem命令(gem命令是从rubygem仓库安装软件,类似yum从yum仓库安装软件)

[root@localhost ~]# yum -y install ruby rubygems ruby-devel gcc

2、查看gem默认源

[root@localhost ~]# gem source list
*** CURRENT SOURCES ***
https://rubygems.org/

3、添加阿里云rubygems仓库

[root@localhost ~]# gem sources -a http://mirrors.aliyun.com/rubygems/
http://mirrors.aliyun.com/rubygems/ added to sources

4、移除默认的国外源

[root@localhost ~]# gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources

5、查看gem源是否为阿里云

[root@localhost ~]# gem source list
*** CURRENT SOURCES ***
http://mirrors.aliyun.com/rubygems/

6、安装fpm工具

[root@localhost ~]# gem install fpm
Fetching: cabin-0.9.0.gem (100%)
Successfully installed cabin-0.9.0
Fetching: backports-3.13.0.gem (100%)
Successfully installed backports-3.13.0
Fetching: arr-pm-0.0.10.gem (100%)
Successfully installed arr-pm-0.0.10
Fetching: clamp-1.0.1.gem (100%)
Successfully installed clamp-1.0.1
Fetching: ffi-1.10.0.gem (100%)
Building native extensions.  This could take a while...
Successfully installed ffi-1.10.0
Fetching: childprocess-0.9.0.gem (100%)
Successfully installed childprocess-0.9.0
Fetching: io-like-0.3.0.gem (100%)
Successfully installed io-like-0.3.0
Fetching: ruby-xz-0.2.3.gem (100%)
Successfully installed ruby-xz-0.2.3
Fetching: stud-0.0.23.gem (100%)
Successfully installed stud-0.0.23
Fetching: mustache-0.99.8.gem (100%)
Successfully installed mustache-0.99.8
Fetching: insist-1.0.0.gem (100%)
Successfully installed insist-1.0.0
Fetching: dotenv-2.7.2.gem (100%)
Successfully installed dotenv-2.7.2
Fetching: pleaserun-0.0.30.gem (100%)
Successfully installed pleaserun-0.0.30
Fetching: fpm-1.11.0.gem (100%)
Successfully installed fpm-1.11.0
Parsing documentation for cabin-0.9.0
Installing ri documentation for cabin-0.9.0
Parsing documentation for backports-3.13.0
Installing ri documentation for backports-3.13.0
Parsing documentation for arr-pm-0.0.10
Installing ri documentation for arr-pm-0.0.10
Parsing documentation for clamp-1.0.1
Installing ri documentation for clamp-1.0.1
Parsing documentation for ffi-1.10.0
Installing ri documentation for ffi-1.10.0
Parsing documentation for childprocess-0.9.0
Installing ri documentation for childprocess-0.9.0
Parsing documentation for io-like-0.3.0
Installing ri documentation for io-like-0.3.0
Parsing documentation for ruby-xz-0.2.3
Installing ri documentation for ruby-xz-0.2.3
Parsing documentation for stud-0.0.23
Installing ri documentation for stud-0.0.23
Parsing documentation for mustache-0.99.8
Installing ri documentation for mustache-0.99.8
Parsing documentation for insist-1.0.0
Installing ri documentation for insist-1.0.0
Parsing documentation for dotenv-2.7.2
Installing ri documentation for dotenv-2.7.2
Parsing documentation for pleaserun-0.0.30
Installing ri documentation for pleaserun-0.0.30
Parsing documentation for fpm-1.11.0
Installing ri documentation for fpm-1.11.0
14 gems installed

题外话:如果操作系统为CentOS6,可能会提示ruby版本问题,这个时候我们指定安装老版本(因为升级到新版本很麻烦)

[root@localhost ~]# gem install fpm
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
ERROR:  Error installing fpm:
    ruby-xz requires Ruby version >= 1.9.3.
[root@localhost ~]# gem install fpm -v 1.4.0

7、查看版本:

[root@localhost ~]# gem -v
2.0.14.1

8、zabbix agent编辑安装

参考官方cases: https://fpm.readthedocs.io/en/latest/use-cases/make-install.html

编辑安装zabbix agent:

[root@localhost ~]# groupadd zabbix
[root@localhost ~]# useradd -g zabbix zabbix
[root@localhost ~]# mkdir -p /tmp/installdir
[root@localhost ~]# mkdir -p /tmp/installdir/etc/rc.d/init.d/
[root@localhost ~]# mkdir -p /root/soft/
[root@localhost ~]# cd /root/soft/
[root@localhost soft]# yum install pcre pcre-devel openssl openssl-devel -y
[root@localhost soft]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.4/zabbix-4.0.4.tar.gz
[root@localhost soft]# tar -zxvf zabbix-4.0.4.tar.gz
[root@localhost soft]# cd zabbix-4.0.4
[root@localhost zabbix-4.0.4]# ./configure --prefix=/home/zabbix --enable-agent --with-openssl

#将程序安装在/tmp/installdir目录,并拷贝定制文件
[root@localhost zabbix-4.0.4]# make install DESTDIR=/tmp/installdir
[root@localhost zabbix-4.0.4]# cp /root/soft/zabbix-4.0.4/misc/init.d/fedora/core/zabbix_agentd /tmp/installdir/etc/rc.d/init.d/
[root@localhost zabbix-4.0.4]# vim /tmp/installdir/etc/rc.d/init.d/zabbix_agentd
BASEDIR=/home/zabbix

#上传LLD监控端口脚本和相关文件
[root@localhost zabbix-4.0.4]# mkdir -p /tmp/installdir/home/zabbix/script/monitor_port
[root@localhost zabbix-4.0.4]# cd /tmp/installdir/home/zabbix/script/monitor_port
[root@localhost monitor_port]# 上传脚本和相关文件

#上传LLD监控进程脚本和相关文件
[root@localhost zabbix-4.0.4]# mkdir -p /tmp/installdir/home/zabbix/script/monitor_proc
[root@localhost zabbix-4.0.4]# cd /tmp/installdir/home/zabbix/script/monitor_proc
[root@localhost monitor_proc]# 上传脚本和相关文件

#上传LLD监控jvm脚本和相关文件
[root@localhost zabbix-4.0.4]# mkdir -p /tmp/installdir/home/zabbix/script/jvm
[root@localhost zabbix-4.0.4]# cd /tmp/installdir/home/zabbix/script/jvm
[root@localhost jvm]# 上传脚本和相关文件

#上传ssl证书文件
[root@localhost zabbix-4.0.4]# mkdir -p /tmp/installdir/home/zabbix/zabbix_ssl
[root@localhost zabbix-4.0.4]# cd /tmp/installdir/home/zabbix/zabbix_ssl
[root@localhost zabbix_ssl]# 上传ssl证书文件
[root@localhost zabbix-4.0.4]# cd /tmp/installdir/home/zabbix/etc/zabbix_agentd.conf.d/
[root@localhost zabbix_agentd.conf.d]# 上传相关配置文件

9、zabbix agent脚本

#创建安装前的脚本:
[root@localhost ~]# vim /root/soft/zabbix-4.0.4/pre_install.sh
#!/bin/bash
groupadd zabbix
useradd -g zabbix zabbix

#创建安装后的脚本:
[root@localhost ~]# vim /root/soft/zabbix-4.0.4/post_install.sh
#!/bin/bash
mkdir -p /home/zabbix/logs
chown -R zabbix.zabbix /home/zabbix
chmod -R 755 /home/zabbix
sed -i "s/LogFile=\/tmp\/zabbix_agentd\.log/LogFile=\/home\/zabbix\/logs\/zabbix_agentd.log/g" /home/zabbix/etc/zabbix_agentd.conf
sed -i "s/ServerActive=127\.0\.0\.1/ServerActive=192\.168\.7\.10/g" /home/zabbix/etc/zabbix_agentd.conf
ipaddr=`ip a | grep -w "inet" | egrep "eno3|eth0" |sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g'`
sed -i "s/Hostname=Zabbix server/Hostname=$ipaddr/g" /home/zabbix/etc/zabbix_agentd.conf
sed -i "s/Timeout=3/Timeout=30/g" /home/zabbix/etc/zabbix_agentd.conf
echo "UnsafeUserParameters=1" >>/home/zabbix/etc/zabbix_agentd.conf
echo "EnableRemoteCommands=1" >>/home/zabbix/etc/zabbix_agentd.conf
echo "Include=/home/zabbix/etc/zabbix_agentd.conf.d/" >>/home/zabbix/etc/zabbix_agentd.conf
echo "PidFile=/home/zabbix/zabbix_agentd.pid" >>/home/zabbix/etc/zabbix_agentd.conf
echo "LogFileSize=0" >>/home/zabbix/etc/zabbix_agentd.conf
echo "StartAgents=0" >>/home/zabbix/etc/zabbix_agentd.conf
echo "HostMetadataItem=system.uname" >>/home/zabbix/etc/zabbix_agentd.conf
echo "User=zabbix" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSConnect=cert" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSAccept=cert" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSCAFile=/home/zabbix/zabbix_ssl/ca.crt" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSCertFile=/home/zabbix/zabbix_ssl/client.crt" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSKeyFile=/home/zabbix/zabbix_ssl/client.key" >>/home/zabbix/etc/zabbix_agentd.conf
systemctl daemon-reload
/etc/init.d/zabbix_agentd start
systemctl enable zabbix_agentd

#创建卸载前的脚本:
[root@localhost ~]# vim /root/soft/zabbix-4.0.4/pre_uninstall.sh
#!/bin/bash
systemctl stop zabbix_agentd

#创建卸载后的脚本:
[root@localhost ~]# vim /root/soft/zabbix-4.0.4/post_uninstall.sh
#!/bin/bash
userdel -r zabbix
rm -rf /etc/rc.d/init.d/zabbix_agentd
rm -rf /home/zabbix

10、打包rpm

#fpm帮助:
[root@localhost installdir]# fpm -help

#安装rpm-build工具(fpm依赖rpm-build):
[root@localhost ~]# yum -y install rpm-build

#fpm打包命令:
#el6:
[root@localhost ~]# fpm -f -s dir -t rpm -n zabbix-agent --epoch 0 -v 4.0.4 -C /tmp/installdir  -p /tmp/ -d 'pcre' -d 'pcre-devel' -d 'openssl' -d 'openssl-devel' --verbose --category 'Applications/Internet' --description 'Zabbix Agent' --url 'http://www.zabbix.com/' --license 'BSD' -m 'zabbix' --iteration '1.el7' --pre-install /root/soft/zabbix-4.0.4/pre_install.sh --post-install /root/soft/zabbix-4.0.4/post_install.sh --pre-uninstall /root/soft/zabbix-4.0.4/pre_uninstall.sh --post-uninstall /root/soft/zabbix-4.0.4/post_uninstall.sh --no-rpm-sign

#el7:
[root@localhost ~]# fpm -f -s dir -t rpm -n zabbix-agent --epoch 0 -v 4.0.4 -C /tmp/installdir  -p /tmp/ -d 'pcre' -d 'pcre-devel' -d 'openssl' -d 'openssl-devel' --verbose --category 'Applications/Internet' --description 'Zabbix Agent' --url 'http://www.zabbix.com/' --license 'BSD' -m 'zabbix' --iteration '1.el7' --pre-install /root/soft/zabbix-4.0.4/pre_install.sh --post-install /root/soft/zabbix-4.0.4/post_install.sh --pre-uninstall /root/soft/zabbix-4.0.4/pre_uninstall.sh --post-uninstall /root/soft/zabbix-4.0.4/post_uninstall.sh --no-rpm-sign
No args, but -s dir and -C are given, assuming '.' as input {:level=>:info}
Setting workdir {:workdir=>"/tmp", :level=>:info}
Setting from flags: category=Applications/Internet {:level=>:info}
Setting from flags: description=Zabbix Agent {:level=>:info}
Setting from flags: epoch=0 {:level=>:info}
Setting from flags: iteration=1.el7 {:level=>:info}
Setting from flags: license=BSD {:level=>:info}
Setting from flags: maintainer=zabbix {:level=>:info}
Setting from flags: name=zabbix-agent {:level=>:info}
Setting from flags: url=http://www.zabbix.com/ {:level=>:info}
Setting from flags: version=4.0.4 {:level=>:info}
Converting dir to rpm {:level=>:info}
Reading template {:path=>"/usr/local/share/gems/gems/fpm-1.11.0/templates/rpm.erb", :level=>:info}
Running rpmbuild {:args=>["rpmbuild", "-bb", "--define", "buildroot /tmp/package-rpm-build-517c280305ab2eae42f96b512d65d20358a8c8b3917d4073866670c3af72/BUILD", "--define", "_topdir /tmp/package-rpm-build-517c280305ab2eae42f96b512d65d20358a8c8b3917d4073866670c3af72", "--define", "_sourcedir /tmp/package-rpm-build-517c280305ab2eae42f96b512d65d20358a8c8b3917d4073866670c3af72", "--define", "_rpmdir /tmp/package-rpm-build-517c280305ab2eae42f96b512d65d20358a8c8b3917d4073866670c3af72/RPMS", "--define", "_tmppath /tmp", "/tmp/package-rpm-build-517c280305ab2eae42f96b512d65d20358a8c8b3917d4073866670c3af72/SPECS/zabbix-agent.spec"], :level=>:info}
执行(%prep): /bin/sh -e /tmp/rpm-tmp.hia3sU {:level=>:info}
执行(%build): /bin/sh -e /tmp/rpm-tmp.h3UqVw {:level=>:info}
执行(%install): /bin/sh -e /tmp/rpm-tmp.JwnQo9 {:level=>:info}
处理文件:zabbix-agent-4.0.4-1.el7.x86_64 {:level=>:info}
Provides: zabbix-agent = 0:4.0.4-1.el7 zabbix-agent(x86-64) = 0:4.0.4-1.el7 {:level=>:info}
Requires(interp): /bin/sh /bin/sh /bin/sh /bin/sh {:level=>:info}
Requires(rpmlib): rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rpmlib(CompressedFileNames) <= 3.0.4-1 {:level=>:info}
Requires(pre): /bin/sh {:level=>:info}
Requires(post): /bin/sh {:level=>:info}
Requires(preun): /bin/sh {:level=>:info}
Requires(postun): /bin/sh {:level=>:info}
写道:/tmp/package-rpm-build-517c280305ab2eae42f96b512d65d20358a8c8b3917d4073866670c3af72/RPMS/x86_64/zabbix-agent-4.0.4-1.el7.x86_64.rpm {:level=>:info}
执行(%clean): /bin/sh -e /tmp/rpm-tmp.l34fPv {:level=>:info}
Created package {:path=>"/tmp/zabbix-agent-4.0.4-1.el7.x86_64.rpm"}

11、验证rpm包

[root@localhost ~]# cd /tmp

[root@localhost tmp]# rpm -qpi zabbix-agent-4.0.4-1.el7.x86_64.rpm
Name        : zabbix-agent
Epoch       : 0
Version     : 4.0.4
Release     : 1.el7
Architecture: x86_64
Install Date: (not installed)
Group       : Applications/Internet
Size        : 2970841
License     : BSD
Signature   : (none)
Source RPM  : zabbix-agent-4.0.4-1.el7.src.rpm
Build Date  : 2019年04月18日 星期四 11时13分27秒
Build Host  : localhost
Relocations : /
Packager    : zabbix
Vendor      : root@localhost.localdomain
URL         : http://www.zabbix.com/
Summary     : Zabbix Agent
Description :
Zabbix Agent

[root@localhost tmp]# rpm -qpl zabbix-agent-4.0.4-1.el7.x86_64.rpm
/etc/rc.d/init.d/zabbix_agentd
/home/zabbix/bin/zabbix_get
/home/zabbix/bin/zabbix_sender
/home/zabbix/etc/zabbix_agentd.conf
/home/zabbix/etc/zabbix_agentd.conf.d/java
/home/zabbix/etc/zabbix_agentd.conf.d/monitor_port
/home/zabbix/etc/zabbix_agentd.conf.d/monitor_proc
/home/zabbix/lib/modules
/home/zabbix/sbin/zabbix_agentd
/home/zabbix/script/jvm/cmdline-jmxclient-0.10.3.jar
/home/zabbix/script/jvm/jvm.py
/home/zabbix/script/jvm/tomcat.py
/home/zabbix/script/monitor_port/discovertcpport.sh
/home/zabbix/script/monitor_port/portlist.txt
/home/zabbix/script/monitor_proc/discoveryproc.sh
/home/zabbix/script/monitor_proc/proclist.txt
/home/zabbix/share/man/man1/zabbix_get.1
/home/zabbix/share/man/man1/zabbix_sender.1
/home/zabbix/share/man/man8/zabbix_agentd.8
/home/zabbix/zabbix_ssl/ca.crt
/home/zabbix/zabbix_ssl/client.crt
/home/zabbix/zabbix_ssl/client.key

[root@localhost tmp]# rpm -qp --scripts zabbix-agent-4.0.4-1.el7.x86_64.rpm
preinstall scriptlet (using /bin/sh):
#!/bin/bash
groupadd zabbix
useradd -g zabbix zabbix
postinstall scriptlet (using /bin/sh):
#!/bin/bash
mkdir -p /home/zabbix/logs
chown -R zabbix.zabbix /home/zabbix
chmod -R 755 /home/zabbix
sed -i "s/LogFile=\/tmp\/zabbix_agentd\.log/LogFile=\/home\/zabbix\/logs\/zabbix_agentd.log/g" /home/zabbix/etc/zabbix_agentd.conf
sed -i "s/ServerActive=127\.0\.0\.1/ServerActive=192\.168\.7\.10/g" /home/zabbix/etc/zabbix_agentd.conf
ipaddr=`ip a | grep -w "inet" | grep eno16777736 |sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g'`
sed -i "s/Hostname=Zabbix server/Hostname=$ipaddr/g" /home/zabbix/etc/zabbix_agentd.conf
sed -i "s/Timeout=3/Timeout=30/g" /home/zabbix/etc/zabbix_agentd.conf
echo "UnsafeUserParameters=1" >>/home/zabbix/etc/zabbix_agentd.conf
echo "EnableRemoteCommands=1" >>/home/zabbix/etc/zabbix_agentd.conf
echo "Include=/home/zabbix/etc/zabbix_agentd.conf.d/" >>/home/zabbix/etc/zabbix_agentd.conf
echo "PidFile=/home/zabbix/zabbix_agentd.pid" >>/home/zabbix/etc/zabbix_agentd.conf
echo "LogFileSize=0" >>/home/zabbix/etc/zabbix_agentd.conf
echo "StartAgents=0" >>/home/zabbix/etc/zabbix_agentd.conf
echo "HostMetadataItem=system.uname" >>/home/zabbix/etc/zabbix_agentd.conf
echo "User=zabbix" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSConnect=cert" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSAccept=cert" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSCAFile=/home/zabbix/zabbix_ssl/ca.crt" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSCertFile=/home/zabbix/zabbix_ssl/client.crt" >>/home/zabbix/etc/zabbix_agentd.conf
echo "TLSKeyFile=/home/zabbix/zabbix_ssl/client.key" >>/home/zabbix/etc/zabbix_agentd.conf
systemctl daemon-reload
systemctl start zabbix_agentd
systemctl enable zabbix_agentd
preuninstall scriptlet (using /bin/sh):
#!/bin/bash
systemctl stop zabbix_agentd
pkill -9 zabbix_agentd
postuninstall scriptlet (using /bin/sh):
#!/bin/bash
userdel -r zabbix
rm -rf /etc/rc.d/init.d/zabbix_agentd
rm -rf /home/zabbix

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

微商团队管理实战手册

微商团队管理实战手册

杜一凡 / 人民邮电出版社 / 2015-11 / 45.00元

回顾淘宝,用了10年时间才发展了不到1000万的卖家,再看微商,其仅一年时间就拥有了超过1000万的卖家。进入2015年,微商的发展之路虽有小坎坷,但前景依然被看好。然而任何一个想要做大、做强的微商都要以团队形式来发展,独立的个体只会举步维艰。 本书全面解读微商团队管理的营销书。全书共分为六章,分别从微商团队的商业秘密、微商团队的战略布局、管理基本功、建立高效团队、精通管理工具、未来发展等方......一起来看看 《微商团队管理实战手册》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换