cd /usr/local mkdir prometheus wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz
tar -zxvf prometheus-2.37.0.linux-amd64.tar.gz mv -f prometheus-2.37.0.linux-amd64 prometheus
# my global config global: scrape_interval: 15s # 将抓取间隔设置为每 15 秒。 默认为每 1 分钟。 evaluation_interval: 15s # 每 15 秒评估一次规则。 默认值为每 1 分钟。 # scrape_timeout 设置为全局默认值(10 秒)。 # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # 如果有alertmanager,则需要取消下面这一行注释 # - alertmanager:9093 # 加载规则一次并根据全局“evaluation_interval”定期评估它们。 rule_files: # - "first_rules.yml" # - "second_rules.yml" # 一个抓取配置,只包含一个要抓取的端点: # 这里是prometheus本身。 scrape_configs: # job名称作为标签 `job=<job_name>` 添加到从此配置中抓取的任何时间序列中。 - job_name: "prometheus" # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ["localhost:9090"] #可以添加新的job,需要设置job名称和对应的IP、端口 - job_name: "consul_exporter" static_configs: - targets: ["localhost:9107"]
启动服务
nohup /usr/local/prometheus/prometheus/prometheus --log.level=info --config.file="/usr/local/prometheus/prometheus/prometheus.yml" >/usr/local/prometheus/prometheus/prometheus.log 2>&1 &
systemctl status firewalld firewall-cmd --zone=public --add-port=9090/tcp --permanent firewall-cmd --reload firewall-cmd --zone=public --query-port=9090/tcp
浏览器访问
http://IP:9090/
touch prometheus.sh && chmod +x prometheus.sh
#!/bin/bash #用于安装prometheus #版本号 PROMETHEUS_VER=2.37.0 #安装位置 PROMETHEUS_DIR=/app/prometheus #安装包存放路径 INSTALL_PACKAGE=/app/install #日志文件 LOG_FILE=/tmp/prometheus.log # PROMETHEUS的用户名 PROMETHEUS_USER=prometheus # PROMETHEUS监听端口 PROMETHEUS_PORT=9090 check_err() { if [ $? -ne 0 ]; then echo "$1" exit 1 else echo "$2" fi } start() { [ ! -d /app ] && mkdir -p /app #检查是否存在wget命令,没有则安装 type wget &> /dev/null if [ $? -ne 0 ]; then yum install wget -y check_err "\033[31myum安装依赖包 wget 失败,请检查\033[0m" "\033[36myum安装依赖包 wget 成功\033[0m" fi #检查是否存在netstat命令,没有则安装 type netstat &> /dev/null if [ $? -ne 0 ]; then yum install net-tools -y check_err "\033[31myum安装依赖包 net-tools 失败,请检查\033[0m" "\033[36myum安装依赖包 net-tools 成功\033[0m" fi netstat -tln | grep ":$PROMETHEUS_PORT" CHECK_RESULT=$? if [ $CHECK_RESULT -eq 0 ];then echo "PROMETHEUS 已启动,请勿重复启动应用!" exit 1 fi echo "正在启动 PROMETHEUS....." [ ! -d $INSTALL_PACKAGE ] && mkdir -p $INSTALL_PACKAGE [ ! -f $INSTALL_PACKAGE/prometheus-$PROMETHEUS_VER.linux-amd64.tar.gz ] && wget -O $INSTALL_PACKAGE/prometheus-$PROMETHEUS_VER.linux-amd64.tar.gz https://github.com/prometheus/prometheus/releases/download/v$PROMETHEUS_VER/prometheus-$PROMETHEUS_VER.linux-amd64.tar.gz [ ! -d $PROMETHEUS_DIR ] && mkdir -p $PROMETHEUS_DIR count=`ls $PROMETHEUS_DIR | wc -l` if [ $count -gt 0 ]; then echo "目录 $PROMETHEUS_DIR 不为空,不再重复解压压缩包" else tar -xvzf $INSTALL_PACKAGE/prometheus-$PROMETHEUS_VER.linux-amd64.tar.gz --strip-components 1 -C $PROMETHEUS_DIR fi cat /etc/group | grep $PROMETHEUS_USER if [ $? -ne 0 ]; then groupadd -g 10002 $PROMETHEUS_USER fi cat /etc/passwd | grep $PROMETHEUS_USER if [ $? -ne 0 ]; then useradd -u 10002 -g $PROMETHEUS_USER -d /home/$PROMETHEUS_USER -m $PROMETHEUS_USER fi chown -R $PROMETHEUS_USER:$PROMETHEUS_USER $PROMETHEUS_DIR [ ! -f $LOG_FILE ] && touch $LOG_FILE chown -R $PROMETHEUS_USER:$PROMETHEUS_USER $LOG_FILE # 切换用户 su - $PROMETHEUS_USER << EOF # 不加反斜杠,取的值是切换用户前的 nohup $PROMETHEUS_DIR/prometheus --log.level=info --config.file="$PROMETHEUS_DIR/prometheus.yml" >> $LOG_FILE 2>&1 & #每2s检查一次监听端口是否启动,总共检查60次共2分钟,超时未启动则退出 CHECK_RESULT=1 CHECK_COUNT=60 while [ \$CHECK_RESULT -ne 0 ];do if [ \$CHECK_COUNT -gt 0 ];then echo "等待监听端口启动..." sleep 2 netstat -tln | grep ":$PROMETHEUS_PORT" CHECK_RESULT=\$? CHECK_COUNT=\$((\$CHECK_COUNT-1)) else echo "ERROR: 启动$PROMETHEUS_PORT端口超时,请检查!" exit 1 fi done echo "PROMETHEUS 启动成功!" EOF #检查防火墙是否已启动,如果启动则开放端口 systemctl status firewalld > /dev/null if [ $? -eq 0 ];then firewall-cmd --add-port=$PROMETHEUS_PORT/tcp --permanent firewall-cmd --reload fi } stop(){ netstat -tln | grep ":$PROMETHEUS_PORT" if [ $? -ne 0 ]; then echo "PROMETHEUS 未启动!" else echo "PROMETHEUS 正在运行,开始停止应用....." fi su - $PROMETHEUS_USER << EOF PROCESS=`ps aux | grep $PROMETHEUS_DIR/prometheus | grep -v "grep" | awk '{print $2}'` for i in \${PROCESS} do echo "Kill the PROMETHEUS process [ \$i ]" kill -9 \$i done EOF CHECK_RESULT=0 CHECK_COUNT=30 while [ $CHECK_RESULT -eq 0 ];do if [ $CHECK_COUNT -gt 0 ];then echo "等待监听端口关闭..." sleep 2 netstat -tln | grep ":$PROMETHEUS_PORT" CHECK_RESULT=$? CHECK_COUNT=$(($CHECK_COUNT-1)) else echo "ERROR: 关闭$PROMETHEUS_PORT端口超时,请检查!" exit 1 fi done echo "关闭 PROMETHEUS 成功!" } restart(){ stop start } status(){ su $PROMETHEUS_USER -c "ps aux | grep $PROMETHEUS_DIR/prometheus | grep -v \"grep\" | grep -v \"su $PROMETHEUS_USER\"" } remove(){ rm -rf $LOG_FILE rm -rf $PROMETHEUS_DIR userdel -r $PROMETHEUS_USER } add(){ ROWS=`cat $PROMETHEUS_DIR/prometheus.yml | grep -n "scrape_configs" | awk -F ":" '{print $1}'` JOB_NAME=$1 TARGETS=$2 echo "JOB_NAME: $JOB_NAME" echo "TARGETS: $TARGETS" if [[ ! -n "$JOB_NAME" ]] ; then echo "JOB_NAME 不能为空!" exit 1 fi if [[ ! -n "$TARGETS" ]] ; then echo "TARGETS 不能为空!" exit 1 fi CNT=`sed -n "${ROWS}"',$p' $PROMETHEUS_DIR/prometheus.yml | grep "job_name" | grep "$JOB_NAME" | wc -l` if [ $CNT -gt 0 ]; then echo "$JOB_NAME 已添加,请勿重复添加!" exit 1 fi #添加job配置 sed -i "${ROWS}"'a \\ - job_name: "'"${JOB_NAME}"'" \ static_configs: \ - targets: ["'"${TARGETS}"'"]' $PROMETHEUS_DIR/prometheus.yml restart } del(){ JOB_NAME=$1 if [[ ! -n "$JOB_NAME" ]] ; then echo "JOB_NAME 不能为空!" exit 1 fi echo "JOB_NAME: $JOB_NAME" #开始行 ROWS=`cat $PROMETHEUS_DIR/prometheus.yml | grep -n "job_name" | grep -n "$JOB_NAME" | awk -F ":" '{print $2}'` #结束行 ROWE=$(($ROWS+2)) sed -i "${ROWS}"','"${ROWE}"'d' $PROMETHEUS_DIR/prometheus.yml restart } usage () { echo " " echo "请输入 start 启动 PROMETHEUS。" echo "请输入 stop 停止 PROMETHEUS。" echo "请输入 status 获取 PROMETHEUS 状态。" echo "请输入 restart 重启 PROMETHEUS。" echo "请输入 remove 删除 PROMETHEUS。" echo "请输入 add 添加 job 配置。示例:./prometheus.sh add \"consul_exporter\" \"localhost:9107\"(add后面第一个参数是job名称,第二个参数是job名称对应的IP、端口)" echo "请输入 del 删除 job 配置。示例:./prometheus.sh del \"consul_exporter\" (del 后面的参数是job名称)" echo " " } INPUT_ACTIVE=$1 ACTIVE=${INPUT_ACTIVE:=start} case ${ACTIVE} in start) start ;; stop) stop ;; status) status ;; restart) restart ;; remove) remove ;; add) add $2 $3 ;; del) del $2 ;; *) usage ;; esac
./prometheus.sh status ./prometheus.sh start ./prometheus.sh stop ./prometheus.sh restart ./prometheus.sh add "consul_exporter" "localhost:9107" ./prometheus.sh del "consul_exporter"