blob: fd431825a39eab911484add02f923b103b232ced [file] [log] [blame]
David K. Bainbridge7ae4f192016-09-29 09:01:38 -07001#!/bin/bash
2#D provides access to the CORD POD DHCP havesting of IP addresses
3
4PROG=$(echo $(basename $0) | sed -e 's/^cord-/cord /g')
5
6usage() {
7 echo "usage: $PROG <sub-command> [options"
8 echo " go performs a harvest from the DHCP server"
9 echo " list list the currently harvested information"
10 echo " check check the error logs of the harvester for potential isseus"
11 echo " help this message"
12}
13
14COMMAND=$1; shift
15SSH_OPT=
16if [ $CORD_HEAD_NODE != "localhost" -a $CORD_HEAD_NODE != "127.0.0.1" ]; then
17 SSH_OPT="ssh -p $CORD_HEAD_NODE_PORT $CORD_HEAD_NODE_USER@$CORD_HEAD_NODE"
18fi
19case $COMMAND in
20 list)
21 DO_JSON=0
22 while [ $# -gt 0 ]; do
23 case $1 in
24 json|--json|-j)
25 DO_JSON=1
26 ;;
27 help|--help|-h)
28 echo "usage $PROG list [--json|-j]"
29 echo " json display output as JSON object"
30 exit 0
31 ;;
32 *)
33 >&2 "Unknoan option '$1'"
34 echo "usage: $PROG list [--json|-j]"
35 echo " json display output as JSON object"
36 exit 1
37 ;;
38 esac
39 shift
40 done
41
42 if [ "$SSH_OPT x" == " x" -a ! -r /etc/bind/maas/dhcp_harvest.inc ]; then
43 >&2 echo "Unable to find the DHCP harvest file locally, please specify the server option if you are not on the head node."
44 exit 1
45 fi
46 if [ $DO_JSON -eq 1 ]; then
47 $SSH_OPT cat /etc/bind/maas/dhcp_harvest.inc | grep "^[^ ][^ ]*\s\s*IN A\s" | awk 'BEGIN{printf("[")} {printf("{\"name\":\"%s\",\"ip\":\"%s\",\"mac\":\"%s\"}", $1,$4,$6)} END{printf("]")}' | sed -e 's/}{/},{/g'
48 else
49 $SSH_OPT cat /etc/bind/maas/dhcp_harvest.inc | grep "^[^ ][^ ]*\s\s*IN A\s"
50 fi
51 ;;
52 check)
53 RUNNING=$($SSH_OPT docker inspect --format="'{{ .State.Running }}'" harvester)
54 if [ $? -ne 0 ]; then
55 >&2 echo "Unable to execute docker or locate harvester container, if not running on the head node please specify the server address"
56 exit 1
57 fi
58 if [ "$RUNNING" == "false" ]; then
59 >&2 echo "The harvester container is not currently running, results may not be of value"
60 fi
61 OUT=$(mktemp)
62 $SSH_OPT docker logs harvester 2>&1 | grep -v "Warning: Permanently added" > $OUT
63 ERROR_CNT=$(cat $OUT | grep -i error | wc -l)
64 LAST_100=$(cat $OUT | tail -100 | grep -i error | wc -l)
65 ERR_FOUND=$(cat $OUT | tail -100 | grep -i "failed to update DNS server" | wc -l)
66 LAST_ERR=$(cat $OUT | grep -i error | tail -1)
67 rm -f $OUT
68
69 if [ $ERROR_CNT -ne 0 ]; then
70 if [ $LAST_100 -ne 0 ]; then
71 if [ $ERR_FOUND -ne 0 ]; then
72 echo "There is a recent error in the log that may indicate the harvester is unable to update the DNS server"
73 echo "This may be able to be fixed by restarting the DNS server, `sudo service bind9 restart`"
74 echo "Restarting the DNS server will not immediately clear this message"
75 else
76 echo "Recent errors have been found in the log, the last error was '$LAST_ERR'"
77 fi
78 else
79 echo "There are errors in the log, but there are not errors in the last 100 log messages, so the harvester is likely ok"
80 fi
81 else
82 echo "There are no errors in the log, so the harvester is likely OK"
83 fi
84 ;;
85 go)
86 RESULT=$(curl --fail -sSL -XPOST http://$CORD_HEAD_NODE:8954/harvest 2>&1)
87 ERR=$?
88 if [ $ERR -ne 0 ]; then
89 >&2 "ERROR processing request, exit code '$ERR', '$RESULT'"
90 exit $ERR
91 fi
92 case $(echo $RESULT | jq .response | sed -e 's/"//g') in
93 OK)
94 echo "Havest complete"
95 ;;
96 QUIET)
97 echo "Too many requests, please try in a few seconds"
98 ;;
99 *)
100 >&2 "ERROR: unknown response '$RESULT'"
101 exit 1
102 ;;
103 esac
104 ;;
105 help|--help|-h)
106 usage
107 exit 0
108 ;;
109 *)
110 >&2 echo "Unknown subcommand '$COMMAND'"
111 usage
112 exit 1
113 ;;
114esac
115