From 4c5d84de541c0b9d770f10aeeb5fdd814dc0b8da Mon Sep 17 00:00:00 2001 From: richard Date: Fri, 23 Jun 2017 09:13:58 -0400 Subject: [PATCH] add dynamic motd with hostname, ip and other stats --- dkswarm.xai-corp.net.yml | 4 +- inventory.conf | 26 +++ managed_setup.yml | 3 +- managed_updates.yml | 2 + roles/motd/files/cubox.sh | 163 +++++++++++++++++ roles/motd/files/hostname.sh | 2 + roles/motd/files/systats.sh | 167 ++++++++++++++++++ roles/motd/tasks/main.yml | 37 ++++ .../templates/xai-corp.net.internal.j2 | 2 +- 9 files changed, 402 insertions(+), 4 deletions(-) create mode 100644 inventory.conf create mode 100644 roles/motd/files/cubox.sh create mode 100644 roles/motd/files/hostname.sh create mode 100644 roles/motd/files/systats.sh create mode 100644 roles/motd/tasks/main.yml diff --git a/dkswarm.xai-corp.net.yml b/dkswarm.xai-corp.net.yml index 1093133..4a9b73a 100644 --- a/dkswarm.xai-corp.net.yml +++ b/dkswarm.xai-corp.net.yml @@ -12,9 +12,9 @@ managers: - dkhost01 - dkhost03 - workers: - - dkhost04 + workers: [] removed: + - dkhost04 - dkhost02 history: 1 diff --git a/inventory.conf b/inventory.conf new file mode 100644 index 0000000..7045c96 --- /dev/null +++ b/inventory.conf @@ -0,0 +1,26 @@ +localhost ansible_connection=local + +[managed] +home ansible_ssh_host=192.168.2.11 +home02 ansible_ssh_host=192.168.2.22 +dkhost01 ansible_ssh_host=192.168.2.41 +#dkhost02 ansible_ssh_host=192.168.2.43 +dkhost03 ansible_ssh_host=192.168.2.53 +#dkhost04 ansible_ssh_host=192.168.2.54 +#logs ansible_ssh_host=192.168.2.42 +cubox-i ansible_ssh_host=192.168.2.12 + +[dkhost] +dkhost01 ansible_ssh_host=192.168.2.41 +#dkhost02 ansible_ssh_host=192.168.2.43 +dkhost03 ansible_ssh_host=192.168.2.53 +#dkhost04 ansible_ssh_host=192.168.2.54 + +[ns] +home02 ansible_ssh_host=192.168.2.22 +cubox-i ansible_ssh_host=192.168.2.12 + +[gfs] +home ansible_ssh_host=192.168.2.11 +cubox-i ansible_ssh_host=192.168.2.12 + diff --git a/managed_setup.yml b/managed_setup.yml index caa0471..9dffe66 100644 --- a/managed_setup.yml +++ b/managed_setup.yml @@ -56,12 +56,13 @@ removes: /home/ubuntu/.bashrc - debug: var=ansible_os_family + run_once: true roles: - novuso.users - user-richard - rsyslog -# - Datadog.datadog #does not support armhf architecture. should switch to fluentd or logstash + - { role: Datadog.datadog, when: ansible_architecture != 'armv7l' } #does not support armhf architecture. should switch to fluentd or logstash tasks: - name: add ansible to sudoers diff --git a/managed_updates.yml b/managed_updates.yml index ac30d85..830f3e0 100644 --- a/managed_updates.yml +++ b/managed_updates.yml @@ -13,5 +13,7 @@ roles: - _install_updates - user-richard + - motd + - { role: Datadog.datadog, when: ansible_architecture != 'armv7l' } #does not support armhf architecture. should switch to fluentd or logstash tasks: diff --git a/roles/motd/files/cubox.sh b/roles/motd/files/cubox.sh new file mode 100644 index 0000000..5ea4ae5 --- /dev/null +++ b/roles/motd/files/cubox.sh @@ -0,0 +1,163 @@ +#!/bin/bash +# +# 30-sysinfo - generate the system information +# Copyright (c) 2015-2017 Igor Pecovnik + +export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + +THIS_SCRIPT="sysinfo" +MOTD_DISABLE="" + +[[ -f /etc/default/armbian-motd ]] && . /etc/default/armbian-motd + +for f in $MOTD_DISABLE; do + [[ $f == $THIS_SCRIPT ]] && exit 0 +done + +# define which hard drive you want to monitor +storage=/dev/sda1 + +# don't edit below here + +function display() { + # $1=name $2=value $3=red_limit $4=minimal_show_limit $5=unit $6=after $7=acs/desc{ + # battery red color is opposite, lower number + if [[ "$1" == "Battery" ]]; then local great="<"; else local great=">"; fi + if [[ -n "$2" && "$2" > "0" && (( "${2%.*}" -ge "$4" )) ]]; then + printf "%-14s%s" "$1:" + if awk "BEGIN{exit ! ($2 $great $3)}"; then echo -ne "\e[0;91m $2"; else echo -ne "\e[0;92m $2"; fi + printf "%-1s%s\x1B[0m" "$5" + printf "%-11s%s\t" "$6" + return 1 + fi +} # display + +function getboardtemp() { + if [ -f /etc/armbianmonitor/datasources/soctemp ]; then + read raw_temp /dev/null) + case ${amb_temp} in + *"find the USB device"*) + echo "" + ;; + *) + amb_temp=$(awk '{print $NF}' <<<$amb_temp | sed 's/C//g') + echo -n "scale=1;${amb_temp}/1" | grep -oE "\-?[[:digit:]]+.[[:digit:]]" + esac +} # ambienttemp + +function get_ip_addresses() { + # return up to 2 IPv4 address(es) comma separated + hostname -I | tr " " "\n" | \ + grep -E "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" | \ + tail -n2 | sed ':a;N;$!ba;s/\n/,/g' +} # get_ip_addresses + +function storage_info() { + # storage info + RootInfo=$(df -h /) + root_usage=$(awk '/\// {print $(NF-1)}' <<<${RootInfo} | sed 's/%//g') + root_total=$(awk '/\// {print $(NF-4)}' <<<${RootInfo}) + StorageInfo=$(df -h $storage 2>/dev/null | grep $storage) + if [ -n "${StorageInfo}" ]; then + storage_usage=$(awk '/\// {print $(NF-1)}' <<<${StorageInfo} | sed 's/%//g') + storage_total=$(awk '/\// {print $(NF-4)}' <<<${StorageInfo}) + [[ "$storage" == */sd* ]] && hdd_temp=$(hddtemp -u C -nq $storage) + fi +} # storage_info + +# query various systems and send some stuff to the background for overall faster execution. +# Works only with ambienttemp and batteryinfo since A20 is slow enough :) +amb_temp=$(ambienttemp &) +ip_address=$(get_ip_addresses &) +batteryinfo +storage_info +getboardtemp +critical_load=$(( 1 + $(grep -c processor /proc/cpuinfo) / 2 )) + +# get uptime, logged in users and load in one take +UptimeString=$(uptime | tr -d ',') +time=$(awk -F" " '{print $3" "$4}' <<<"${UptimeString}") +load="$(awk -F"average: " '{print $2}'<<<"${UptimeString}")" +users="$(awk -F" user" '{print $1}'<<<"${UptimeString}")" +case ${time} in + 1:*) # 1-2 hours + time=$(awk -F" " '{print $3" hour"}' <<<"${UptimeString}") + ;; + *:*) # 2-24 hours + time=$(awk -F" " '{print $3" hours"}' <<<"${UptimeString}") + ;; +esac + +# memory and swap +mem_info=$(LANG=en_US.UTF-8 free -w 2>/dev/null | grep "^Mem" || LANG=en_US.UTF-8 free | grep "^Mem") +memory_usage=$(awk '{printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}' <<<${mem_info}) +memory_total=$(awk '{printf("%d",$2/1024)}' <<<${mem_info}) +swap_info=$(LANG=en_US.UTF-8 free -m | grep "^Swap") +swap_usage=$( (awk '/Swap/ { printf("%3.0f", $3/$2*100) }' <<<${swap_info} 2>/dev/null || echo 0) | tr -c -d '[:digit:]') +swap_total=$(awk '{print $(2)}' <<<${swap_info}) + +# display info +display "System load" "${load%% *}" "${critical_load}" "0" "" "${load#* }" +printf "Up time: \x1B[92m%s\x1B[0m\t\t" "$time" +display "Local users" "${users##* }" "3" "2" "" +echo "" # fixed newline +display "Memory usage" "$memory_usage" "70" "0" " %" " of ${memory_total}MB" +display "Swap usage" "$swap_usage" "10" "0" " %" " of $swap_total""Mb" +printf "IP: " +printf "\x1B[92m%s\x1B[0m" "$ip_address" +echo "" # fixed newline +a=0;b=0;c=0 +display "CPU temp" "$board_temp" "45" "0" "°C" "" ; a=$? +display "HDD temp" "$hdd_temp" "45" "0" "°C" "" ; b=$? +display "Ambient temp" "$amb_temp" "40" "0" "°C" "" ; c=$? +(( ($a+$b+$c) >0 )) && echo "" # new line only if some value is displayed +display "Usage of /" "$root_usage" "90" "1" "%" " of $root_total" +display "storage/" "$storage_usage" "90" "1" "%" " of $storage_total" +display "Battery" "$battery_percent" "20" "1" "%" "$status_battery_text" +echo "" +echo "" diff --git a/roles/motd/files/hostname.sh b/roles/motd/files/hostname.sh new file mode 100644 index 0000000..77d5177 --- /dev/null +++ b/roles/motd/files/hostname.sh @@ -0,0 +1,2 @@ +#!/bin/bash +figlet $(hostname) diff --git a/roles/motd/files/systats.sh b/roles/motd/files/systats.sh new file mode 100644 index 0000000..09212d4 --- /dev/null +++ b/roles/motd/files/systats.sh @@ -0,0 +1,167 @@ +#!/bin/bash +# +# 10-sysinfo - generate the system information +# Copyright (c) 2013 Nick Charlton +# +# Authors: Nick Charlton <hello@nickcharlton.net> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# define which hard drive you want to monitor +storage=/dev/sda1 + + +function display() { + # $1=name $2=value $3=red_limit $4=minimal_show_limit $5=unit $6=after $7=acs/desc{ + # battery red color is opposite, lower number + if [[ "$1" == "Battery" ]]; then local great="<"; else local great=">"; fi + if [[ -n "$2" && "$2" > "0" && (( "${2%.*}" -ge "$4" )) ]]; then + printf "%-14s%s" "$1:" + if awk "BEGIN{exit ! ($2 $great $3)}"; then echo -ne "\e[0;91m $2"; else echo -ne "\e[0;92m $2"; fi + printf "%-1s%s\x1B[0m" "$5" + printf "%-11s%s\t" "$6" + return 1 + fi +} # display + +#function getboardtemp() { +# if [ -f /etc/armbianmonitor/datasources/soctemp ]; then +# read raw_temp /dev/null) +# case ${amb_temp} in +# *"find the USB device"*) +# echo "" +# ;; +# *) +# amb_temp=$(awk '{print $NF}' <<<$amb_temp | sed 's/C//g') +# echo -n "scale=1;${amb_temp}/1" | grep -oE "\-?[[:digit:]]+.[[:digit:]]" +# esac +#} # ambienttemp + +function get_ip_addresses() { + # return up to 2 IPv4 address(es) comma separated + hostname -I | tr " " "\n" | \ + grep "192.168." | \ + tail -n2 | sed ':a;N;$!ba;s/\n/,/g' +} # get_ip_addresses +ip_address=$(get_ip_addresses &) + +## storage +#function storage_info() { +# # storage info +# RootInfo=$(df -h /) +# root_usage=$(awk '/\// {print $(NF-1)}' <<<${RootInfo} | sed 's/%//g') +# root_total=$(awk '/\// {print $(NF-4)}' <<<${RootInfo}) +# StorageInfo=$(df -h $storage 2>/dev/null | grep $storage) +# if [ -n "${StorageInfo}" ]; then +# storage_usage=$(awk '/\// {print $(NF-1)}' <<<${StorageInfo} | sed 's/%//g') +# storage_total=$(awk '/\// {print $(NF-4)}' <<<${StorageInfo}) +# [[ "$storage" == */sd* ]] && hdd_temp=$(hddtemp -u C -nq $storage) +# fi +#} # storage_info +#storage_info +root_usage=`df -h / | awk '/\// {print $(NF-1)}'` + + + +## System info +date=`date` +UptimeString=$(uptime | tr -d ',') +time=$(awk -F" " '{print $3" "$4}' <<<"${UptimeString}") +load="$(awk -F"average: " '{print $2}'<<<"${UptimeString}")" +users="$(awk -F" user" '{print $1}'<<<"${UptimeString}")" +critical_load=$(( 1 + $(grep -c processor /proc/cpuinfo) / 2 )) +processes=`ps aux | wc -l` + +## memory and swap +mem_info=$(LANG=en_US.UTF-8 free -w 2>/dev/null | grep "^Mem" || LANG=en_US.UTF-8 free | grep "^Mem") +memory_usage=$(awk '{printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}' <<<${mem_info}) +memory_total=$(awk '{printf("%d",$2/1024)}' <<<${mem_info}) +swap_info=$(LANG=en_US.UTF-8 free -m | grep "^Swap") +swap_usage=$( (awk '/Swap/ { printf("%3.0f", $3/$2*100) }' <<<${swap_info} 2>/dev/null || echo 0) | tr -c -d '[:digit:]') +swap_total=$(awk '{print $(2)}' <<<${swap_info}) +swap_usage=`free -m | awk '/Swap:/ { printf("%3.1f%%", $3/$2*100) }'` + + +#batteryinfo +#getboardtemp + +# DISPLAY +echo "System information as of: $date" +echo +display "System load" "${load%% *}" "${critical_load}" "0" "" "${load#* }" +printf "Up time: \x1B[92m%s\x1B[0m\t\t" "$time" +echo "" # fixed newline +display "Memory usage" "$memory_usage" "70" "0" " %" " of ${memory_total}MB" +display "Swap usage" "$swap_usage" "10" "0" " %" " of ${swap_total}Mb" +echo "" +display "Usage of /" "$root_usage" "90" "1" "%" " of $root_total" +printf "IP: " +printf "\x1B[92m%s\x1B[0m" "$ip_address" +echo "" +display "Local users" "${users##* }" "3" "0" "" +display "Processes" "${processes##* }" "150" "100" "" +echo "" + +#a=0;b=0;c=0 +#display "CPU temp" "$board_temp" "45" "0" "°C" "" ; a=$? +#display "HDD temp" "$hdd_temp" "45" "0" "°C" "" ; b=$? +#display "Ambient temp" "$amb_temp" "40" "0" "°C" "" ; c=$? +#(( ($a+$b+$c) >0 )) && echo "" # new line only if some value is displayed + diff --git a/roles/motd/tasks/main.yml b/roles/motd/tasks/main.yml new file mode 100644 index 0000000..b2a8030 --- /dev/null +++ b/roles/motd/tasks/main.yml @@ -0,0 +1,37 @@ +--- +# main tasks for setting up motd dynamic shell header + + +- name: Install the nginx packages + apt: + name: "{{ item }}" + update_cache: yes + cache_valid_time: 3600 + state: latest + with_items: + - lsb-release + - figlet + - update-motd + - lm-sensors + when: "ansible_nodename != 'cubox-i'" + + +- name: remove help text + file: + state: absent + path: "{{ item }}" + with_items: + - /etc/update-motd.d/10-help-text + - /etc/update-motd.d/51-cloudguest + when: "ansible_nodename != 'cubox-i'" + + +- name: add new info + copy: + src: "{{ item.src }}" + dest: /etc/update-motd.d/{{ item.dest }} + mode: 755 + with_items: + - { src: hostname.sh, dest: 10-hostname } + - { src: systats.sh, dest: 11-sysstats} + when: "ansible_nodename != 'cubox-i'" diff --git a/roles/ns.xai-corp.net/templates/xai-corp.net.internal.j2 b/roles/ns.xai-corp.net/templates/xai-corp.net.internal.j2 index 1cc012a..fe4fea2 100644 --- a/roles/ns.xai-corp.net/templates/xai-corp.net.internal.j2 +++ b/roles/ns.xai-corp.net/templates/xai-corp.net.internal.j2 @@ -43,7 +43,7 @@ gluster IN A 192.168.2.12 dkhost IN A 192.168.2.41 ;dkhost IN A 192.168.2.43 dkhost IN A 192.168.2.53 -dkhost IN A 192.168.2.54 +;dkhost IN A 192.168.2.54 ; service domains fs IN CNAME dkhost