248 lines
5.8 KiB
Bash
248 lines
5.8 KiB
Bash
#!/bin/bash
|
|
|
|
#############VARIABLE LAND#############
|
|
ROOT=(-e 'ansible_become=false')
|
|
HOSTS_FILE="/root/templates/root.ini"
|
|
PASSWORD=(-k)
|
|
PLAYBOOK=/root/playbooks/master.yml
|
|
COLLECTOR_PLAYBOOK=/root/playbooks/update_collector.yml
|
|
PORT=22
|
|
ANSIBLE_PORT=()
|
|
SERVERS_JSON="/root/servers.json"
|
|
LXC_JSON="/root/porygon_lxc_servers.json"
|
|
PORYGON_KEY=/path/2/key
|
|
PORYGON_LXC_IP="1.2.3.4"
|
|
PORYGON_LXC_KEY="/path/2/key"
|
|
ALL_SERVERS_INI="/root/all_servers.ini"
|
|
|
|
|
|
###########FUNCTIONS#############
|
|
intro() {
|
|
cat "/root/templates/porygon.txt"
|
|
echo "management tool for adding a new server to the porygon monitoring system"
|
|
echo "assumes by default the ssh port is 22, root is the user and password auth is employed"
|
|
echo -e "run porygon-management -h to see help and other options\n"
|
|
}
|
|
|
|
main() {
|
|
read -p "enter the server's hostname: " HOSTNAME
|
|
read -p "enter the server's IP: " IP
|
|
|
|
echo "trusting remote ssh host key"
|
|
ssh-keyscan -P "$PORT" -H "$IP" >> /root/.ssh/known_hosts || {
|
|
echo "failed to trust remote host key"
|
|
exit 1
|
|
}
|
|
|
|
check
|
|
|
|
file_making
|
|
|
|
ansible-playbook -i /root/hosts.ini "$PLAYBOOK" "${PASSWORD[@]}" "${ANSIBLE_PORT[@]}" "${ROOT[@]}" "${SUDO[@]}" "${APPEND[@]}" || {
|
|
echo "failed to run ansible playbook"
|
|
exit 1
|
|
}
|
|
|
|
echo -e "\nansible playbook ran successfully\nporygon monitoring now set up on $HOSTNAME\n\\(^0^)/ LET'S GO\n"
|
|
|
|
json_handling
|
|
|
|
web_handling
|
|
|
|
echo "running scraper.py"
|
|
/root/scraper.py
|
|
|
|
sed -i "/^\[servers:vars\]/i ${IP} ansible_port=${PORT}" "$ALL_SERVERS_INI"
|
|
|
|
echo -e "\nall done, all successful\n\\(^0^)/"
|
|
TO_ADD=$(sed 's/\$HOSTNAME/'"$HOSTNAME"'/g' /root/templates/main-page-paste.html)
|
|
echo -e "place the following into /var/www/porygon/index.html\n\n$TO_ADD"
|
|
}
|
|
|
|
check() {
|
|
|
|
echo -e "\nis this correct?\n
|
|
HOSTNAME = "$HOSTNAME"
|
|
IP = "$IP"
|
|
y/n - q to quit"
|
|
|
|
read ANSWER
|
|
|
|
case "$ANSWER" in
|
|
y)
|
|
;;
|
|
n)
|
|
main
|
|
;;
|
|
q)
|
|
echo "exiting...."
|
|
exit 0
|
|
;;
|
|
*)
|
|
check
|
|
;;
|
|
esac
|
|
|
|
}
|
|
|
|
file_making() {
|
|
REPLACE='s/\$IP/'"$IP"'/g; s/\$USER/'"$USER"'/g'
|
|
echo "creating hosts.ini file"
|
|
sed "$REPLACE" "$HOSTS_FILE" > /root/hosts.ini || {
|
|
echo failed to create hosts.ini
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
json_handling() {
|
|
|
|
echo "backing up the servers.json file"
|
|
|
|
cp "$SERVERS_JSON" "$SERVERS_JSON".old
|
|
|
|
echo "updating servers.json"
|
|
|
|
jq --arg hostname "$HOSTNAME" \
|
|
--arg ip "$IP" \
|
|
--arg port "$PORT" \
|
|
'. + {($hostname): {"ip": $ip, "port": $port}}' "$SERVERS_JSON" > /tmp/servers.json || {
|
|
echo "failed to update servers.json file"
|
|
exit 1
|
|
}
|
|
|
|
mv /tmp/servers.json "$SERVERS_JSON"
|
|
|
|
echo "updating the servers.json file for porygon-lxc"
|
|
|
|
jq --arg hostname "$HOSTNAME" \
|
|
--arg ip "$IP" \
|
|
'. + {$hostname: $ip}' \
|
|
"$LXC_JSON" > /tmp/servers.json || {
|
|
echo "failed to update porygon lxc json file"
|
|
exit 1
|
|
}
|
|
|
|
|
|
mv /tmp/servers.json "$LXC_JSON"
|
|
|
|
echo "copying over new json to porygon lxc"
|
|
|
|
ssh -i "$PORYGON_LXC_KEY" root@"$PORYGON_LXC_IP" 'cp /root/servers.json /root/servers.json.old'
|
|
scp -i "$PORYGON_LXC_KEY" "$LXC_JSON" root@"$PORYGON_LXC_IP":/root/servers.json
|
|
|
|
}
|
|
|
|
web_handling() {
|
|
echo "making directory for apache"
|
|
[ -d /var/www/porygon/"$HOSTNAME" ] || mkdir /var/www/porygon/"$HOSTNAME"
|
|
cp /root/templates/metrics_page.html /var/www/porygon/"$HOSTNAME"/index.html
|
|
}
|
|
|
|
remove_server() {
|
|
|
|
read -p "enter the hostname of the server you would like to remove from porygon management: " HOSTNAME
|
|
|
|
jq -e --arg host "$HOSTNAME" 'has($host)' "$SERVERS_JSON" >/dev/null || {
|
|
echo "$HOSTNAME not found in servers.json file"
|
|
exit 1
|
|
}
|
|
|
|
cp "$SERVERS_JSON" "$SERVERS_JSON".old
|
|
|
|
jq --arg host "$HOSTNAME" 'del(.[$host])' "$SERVERS_JSON" > /tmp/servers.json && mv /tmp/servers.json "$SERVERS_JSON"
|
|
|
|
ssh -i "$PORYGON_LXC_KEY" root@"$PORYGON_LXC_IP" 'cp /root/servers.json /root/servers.json.old'
|
|
|
|
ssh -i "$PORYGON_LXC_KEY" root@"$PORYGON_LXC_IP" \
|
|
"jq --arg host '$HOSTNAME' 'del(.[\$host])' /root/servers.json > /tmp/servers.json \
|
|
&& mv /tmp/servers.json /root/servers.json" || {
|
|
echo "failed to remove $HOSTNAME from porygon lxc"
|
|
exit 1
|
|
}
|
|
|
|
rm -rf /var/www/porygon/"$HOSTNAME"
|
|
|
|
echo -e "\n\nsuccessfully removed $HOSTNAME from porygon monitoring \\(^0^)/\n\n"
|
|
|
|
TO_ADD=$(sed 's/\$HOSTNAME/'"$HOSTNAME"'/g' /root/templates/main-page-paste.html)
|
|
echo -e "remove the following from /var/www/porygon/index.html\n\n$TO_ADD"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
update_collector() {
|
|
|
|
ansible-playbook -i "$ALL_SERVERS_INI" "$COLLECTOR_PLAYBOOK" --private-key "$PORYGON_KEY" "${APPEND[@]}" || {
|
|
echo "failed to run ansible playbook"
|
|
exit 1
|
|
}
|
|
|
|
echo -e "\npushed out new collector.py successfully to all servers\n
|
|
\\(^0^)/"
|
|
exit 0
|
|
}
|
|
|
|
|
|
|
|
######RUNNING INTRO###########
|
|
|
|
intro
|
|
|
|
|
|
#######DEFINING FLAGS############
|
|
while getopts "udkph" flag; do
|
|
case "$flag" in
|
|
|
|
u)
|
|
read -p "enter the username under which ansible with run: " USER
|
|
ROOT=()
|
|
HOSTS_FILE="/root/templates/user.ini"
|
|
SUDO=(--become --ask-become-pass)
|
|
;;
|
|
|
|
c)
|
|
echo "pushing out the modified collector.py to all servers"
|
|
update_collector
|
|
;;
|
|
|
|
d)
|
|
echo -e "ansible debug mode chosen\n"
|
|
APPEND=(-vvv)
|
|
;;
|
|
|
|
k)
|
|
read -p "enter the path to the SSH key used to connect: " KEY
|
|
PASSWORD=(--private-key "$KEY")
|
|
;;
|
|
|
|
p)
|
|
read -p "enter the port to connect over ssh with: " PORT
|
|
ANSIBLE_PORT=(-e ansible_port="$PORT")
|
|
;;
|
|
|
|
r)
|
|
remove_server
|
|
;;
|
|
|
|
h)
|
|
echo -e "Options:\n-c - COPY - push out the latest collector.py to all servers\n\
|
|
-d - DEBUG - runs ansible with verbose (-vvv) flags\n\
|
|
-h - HELP - displays this help page\n-k - KEY - specifies connecting over ssh with a private key\n\
|
|
-p - PORT - specifies which port to connect over ssh with\n\
|
|
-r - REMOVE - specifies which server to remove from porygon monitoring\n\
|
|
-u - USER - specifies a user to connect over ssh and run the ansible playbook with"
|
|
exit 0
|
|
;;
|
|
|
|
*)
|
|
echo "ILLEGAL OPTION :D"
|
|
exit 1
|
|
;;
|
|
|
|
esac
|
|
done
|
|
|
|
########RUNNING MAIN##########
|
|
main |