生产实践:
falcon-agent一键安装脚本
学习技巧:
Shell EOF使用、添加定时任务
脚本内容:
隔离区三方项目机器很多没有人维护,也没有统一监控,需要把基础监控全部收敛到公司falcon监控上,弄一个一键安装脚本来安装方便点,不考虑啥健壮性,能用就行。
#!/bin/bash # # install_falcon_agent.sh # # create by anzhihe@foxmail.com 20221011 # INSTALL_DIR="/home/anzhihe/app" FALCON_DIR="/home/anzhihe/app/falcon_agentd" CONFIG_FILE="${FALCON_DIR}/cfg.json" # falcon-agent下载 download_agent() { if [ ! -d ${FALCON_DIR} ]; then mkdir -p ${INSTALL_DIR} && cd ${INSTALL_DIR} && wget https://file.chegva.com/sre/install_package/falcon_agentd.tar.gz && tar -zxf falcon_agentd.tar.gz else echo "falcon_agentd目录已存在,退出安装!" exit 1 fi } # 添加配置文件 add_config() { hostname=`hostname` cat << EOF > ${CONFIG_FILE} { "debug": false, "filter": true, "hostname": "${hostname}", "safeguard": { "maxMemMByte": 5000 }, "plugin": { "enabled": false, "dir": "${FALCON_DIR}/plugin", "tarball": "http://file.chegva.com/plugin.tar.gz", "logs": "/home/work/falcon_agentd_plugins/logs" }, "heartbeat": { "enabled": false, "addr": "falcon.chegva.com:8443", "isHttpReporter": false, "httpAddr": "http://falcon.chegva.com:8080", "interval": 60, "timeout": 300000 }, "transfer": { "enabled": true, "isGrpcBackend": false, "addr": "transfer.falcon.chegva.com:80", "isHttpSender": true, "httpAddr": "http://falcon.chegva.com:6060/api/push", "interval": 60, "timeout": 5000, "batch": 200 }, "http": { "enabled": true, "listen": ":1988" }, "collector": { "ifacePrefix": ["eth", "em", "vlan","p4p"] } } EOF } # 启动falcon-agent start_agent() { PROCESS_NUM=`ps -ef | grep "falcon-agent" | wc -l` TIME=`date +%Y-%m-%d\ %H:%M:%S` #echo $PROCESS_NUM if [ $PROCESS_NUM -eq 2 ]; then echo $TIME"Falcon agent is runnning" else echo $TIME"Falcon agent is down, will start..." && nohup ${FALCON_DIR}/falcon-agent -c ${CONFIG_FILE} 2>${FALCON_DIR}/var/stderr.log & ps -ef | grep -v grep | grep "falcon-agent" fi } # 添加falcon-agent进程moniter定时任务 add_cron() { # 将crontab定时任务加入到定时任务,agent有时候会自己停了 if grep -q "falcon_agentd" /var/spool/cron/root; then echo "falcon moniter定时任务已经添加!" else echo "*/5 * * * * ${FALCON_DIR}/monitor.sh >> ${FALCON_DIR}/var/app.log" >> /var/spool/cron/root echo "添加falcon moniter定时任务完成" fi } main() { echo "开始安装falcon-agent...."s download_agent && add_config && start_agent && add_cron echo "falcon-agent安装完成,请验证是否有数据显示:https://falcon.chegva.com/#/dashboard \n" } main
相关记录:
# 注入ssh key ansible -i tmp all -m shell -a "echo 'ssh-rsa AAAAB3...' >> /root/.ssh/authorized_keys" # 批量修改主机名 ansible-playbook -i tmp modify_hostname.yml # cat tmp 10.1.1.192 hostname=anzhihe ansible_ssh_pass=anzhihe 10.1.1.198 hostname=anzhihe ansible_ssh_pass=anzhihe # cat modify_hostname.yml --- - hosts: all remote_user: root tasks: - name: set hostname shell: hostname && cat /etc/hosts && hostname {{hostname}} && hostnamectl set-hostname {{hostname}} && hostname # 执行falcon-agent安装脚本 ansible -i tmp all -m script -a 'install_falcon_agent.sh'