#!/bin/bash
IPADDRESS=`curl http://cinfo.global.17m3.com/whoami/geti 2>&1 | grep -Po 'clientIP[" :]+\K[^"]+'`
echo "[info] ${IPADDRESS} start zabbix init"
if [ -d "/data/app" ];then
  echo "[info] data app path ready"
else
  mkdir -p /data/app/
  echo "[create] /data/app/"
fi

if [ -d "/data/logs/zabbix" ];then
  echo "[info] data logs zabbix path ready"
else
  mkdir -p /data/logs/zabbix
  echo "[create] /data/logs/zabbix"
fi
if [ -d "/data/corefile" ];then
  echo "[info] data corefile path ready"
else
  mkdir -p /data/corefile
  echo "[create] /data/corefile"
fi
echo "[download] zabbix download start"
curl http://dhones.om.dianhun.cn/yunwei/zabbix-zhxzj.tar.gz -o /data/app/zabbix.tar.gz && tar -zxvf /data/app/zabbix.tar.gz -C /data/app/ && mkdir /data/app/zabbix/logs
rm -f /data/app/zabbix.tar.gz
echo "[download] zabbix download end"
if [ -f "/data/app/zabbix/etc/zabbix_agentd.conf" ];then
  echo "[info] zabbix_agentd.conf update hostname"
  sed -i 's/SET_HOSTNAME/'"${IPADDRESS}"'/g' /data/app/zabbix/etc/zabbix_agentd.conf
  echo "[info] zabbix_agentd registration system service"
  mv /data/app/zabbix/zabbix_agentd.service /usr/lib/systemd/system/zabbix_agentd.service && systemctl daemon-reload
  echo "[info] zabbix_agentd enable config"
  systemctl enable zabbix_agentd
  echo "[info] zabbix_agentd start"
  systemctl restart zabbix_agentd
  sleep 3
  systemctl status zabbix_agentd
else
  echo "[error] zabbix download fail"
fi