上一篇主要简要科普了一下Prometheus是个什么鬼,这一篇主要介绍如何安装Prometheus。本文安装linux环境为centos7
1、安装go环境
yum install go
2、查看go版本
[root@iZtd8edkkjhg2sxz5wck0rZ ~]# go version
go version go1.11.4 linux/amd64
3、下载Prometheus安装包,可以从官网https://prometheus.io/download/下载所需的安装,也可以执行如下命令
wget https://github.com/prometheus/prometheus/releases/download/v2.7.1/prometheus-2.7.1.linux-amd64.tar.gz
4、解压安装包
tar -zxvf prometheus-2.7.1.linux-amd64.tar.gz
5、重命名(可选)
mv prometheus-2.7.1.linux-amd64 prometheus
6、创建操作Prometheus用户和用户组并赋予执行权限(可选)
chattr -i /etc/group;chattr -i /etc/gshadow;chattr -i /etc/passwd;chattr -i /etc/shadow
groupadd prometheus
useradd -g prometheus prometheus
chattr +i /etc/group;chattr +i /etc/gshadow;chattr +i /etc/passwd;chattr +i /etc/shadow
chown -R prometheus:prometheus /home/prometheus
7、编写prometheus服务脚本,具体脚本参见附录
8、赋予Prometheus脚本用户可执行权限
chmod 744 /etc/init.d/prometheus
9、修改端口号(可选)
在启动的脚本添加如下:
--web.listen-address=:$appPort(修改的端口号)
10、运行Prometheus
service prometheus start|stop|restart|status
11、Prometheus图像界面
本教程的Prometheus的端口号是9191,通过浏览器输入ip:9191
#!/bin/sh
# chkconfig:35 90 3
# description: prometheus server manage.
appPort=9191
isWhliePid=/var/run/prometheusWhile.pid
runPaths=/home/prometheus
function status(){
myPid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
if [ "$myPid" != "" ]; then
echo "Started Prometheus. pid = $myPid"
else
echo "Stopped Prometheus."
fi
}
function start(){
myPid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
if [ ! -z "$myPid" ]; then
echo "Prometheus进程存在,3s后重试,请稍候..."
sleep 3
mypid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
if [ ! -z "$myPid" ]; then
echo "忽略启动命令:Prometheus正在运行"
exit 1
fi
fi
echo "Starting Prometheus..."
su - prometheus -c "nohup /home/prometheus/prometheus --web.listen-address=:$appPort --web.enable-lifecycle --config.file=/home/prometheus/prometheus.yml > /dev/null 2>&1 &"
printf "Waiting for Prometheus..."
while true;
do
lsof -i:$appPort|grep LISTEN > /dev/null 2>&1
if [[ $? == 0 ]] ; then break; fi;
myPid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
if [ -z "$myPid" ]; then
echo -e "\nFailed to start Prometheus."
exit 1
fi
printf ".";
sleep 1;
done
echo
myPid=`lsof -i:$appPort|grep LISTEN|awk '{print $2}'`
echo "running: PID:$myPid"
}
function stop(){
echo "Stopping Prometheus..."
myPid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
if [ "$myPid" != "" ]; then
echo 0 > $isWhliePid
kill $myPid
fi
printf "Waiting for Prometheus..."
while true;
do
myPid=$(ps -ef | grep "$runPaths" | grep -v grep | awk '{print $2}')
if [ "$myPid" = "" ] ; then break; fi;
printf ".";
sleep 1;
done
echo
}
case "$1" in
start)
start
status
;;
stop)
stop
status
;;
restart)
$0 stop
$0 start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit $?
;;
esac