#! /bin/bash ######################## ### System Variables ### ######################## if [ "$(command -v lsb_release)" ]; then OS=$(lsb_release -i | awk '{print $3}' | tr '[:upper:]' '[:lower:]') OSVER=$(lsb_release -r | awk '{print $2}' | awk -F. '{print $1}') else OS=$(grep PRETTY_NAME /etc/os-release | sed 's/PRETTY_NAME=//g' | tr -d '="' | awk '{print $1}' | tr '[:upper:]' '[:lower:]') OSVER=$(grep VERSION_ID /etc/os-release | sed 's/VERSION_ID=//g' | tr -d '="' | awk -F. '{print $1}') fi ########################################################### #### Detect Package Manger from OS and OSVer Variables #### ########################################################### if [[ "${OS}" = amazon || "${OS}" = red ]]; then if [[ "${OSVER}" = 2 || "${OSVER}" = 7 ]]; then pkgmgr="yum -y" else pkgmgr="dnf -y" fi elif [ "${OS}" = ubuntu ]; then pkgmgr="apt -y" fi ############################# #### Check for Directory #### ############################# if [ ! -d "/var/lib/node_exporter/" ]; then mkdir -p /var/lib/node_exporter chown prometheus. /var/lib/node_exporter fi ############################ #### Check for Cron Job #### ############################ if ! crontab -l | grep -q "cpu_usage.sh"; then echo -e "$(crontab -u root -l)\n*/3 * * * * /usr/local/bin/cpu_usage.sh > /var/lib/node_exporter/cpu_usage.prom 2>&1" | crontab -u root - fi if [ ! "$(command -v lsof)" ]; then $pkgmgr install lsof fi touch /usr/local/bin/cpu_usage.sh { echo '#! /bin/bash' echo '' echo '########################################################' echo '### ###' echo '### Description: Expose metrics from cpu by process. ###' echo '### ###' echo '### Phil Connor pconnor@ara.com ###' echo '### Version 2.7.8.020524 ###' echo '### ###' echo '########################################################' echo '' echo '#############################' echo '### Process List Function ###' echo '#############################' echo 'processes_list() {' echo ' {' # shellcheck disable=SC2016 echo ' PList=$(ps aux)' echo '' echo ' while read -r PList' echo ' do' # shellcheck disable=SC2016 echo ' pl=$(/usr/bin/awk '\''{print "node_cpu_usage{process=\""$11"\", pid=\""$2"\", owner=\""$1"\"}", $3}'\'')' # shellcheck disable=SC2016 echo ' done <<< "$PList"' echo '' echo ' echo '\''# HELP node_cpu_usage Usage of CPU by process.'\'' ' echo ' echo '\''# TYPE node_cpu_usage gauge'\'' ' # shellcheck disable=SC2016 echo ' echo "$pl"' echo ' }' echo '}' echo '' echo '##################################' echo '### File Handler List Function ###' echo '##################################' echo 'filehandlers_list() {' echo ' {' # shellcheck disable=SC2016,SC2028 echo ' Fhlist=$(/usr/sbin/lsof | /usr/bin/awk '\''{gsub(/\\/,"",$1)}1 {gsub(/\\|\:/,"",$4)}1 {print $1 " " $2 " " $4}'\'' | sort | uniq -c | sort -rn | head -30)' echo '' echo ' while read -r Fhlist' echo ' do' # shellcheck disable=SC2016 echo ' fhl=$(/usr/bin/awk '\''{print "node_file_handlers{pid=\""$1"\", program=\""$2"\"}", $3}'\'')' # shellcheck disable=SC2016 echo ' done <<< "$Fhlist"' echo ' echo '\''# HELP node_file_handler Usage of File Handlers.'\'' ' echo ' echo '\''# TYPE node_file_handler gauge'\'' ' # shellcheck disable=SC2016 echo ' echo "$fhl"' echo '' # shellcheck disable=SC2016 echo ' fhmax=$(cat /proc/sys/fs/file-nr | awk '\''{print $3}'\'')' # shellcheck disable=SC2016 echo ' fhfree=$(cat /proc/sys/fs/file-nr | awk '\''{print $2}'\'')' # shellcheck disable=SC2016 echo ' fhopen=$(cat /proc/sys/fs/file-nr | awk '\''{print $1}'\'')' echo ' echo '\''# HELP node_max_files Max File Limit Handlers.'\'' ' echo ' echo '\''# TYPE node_max_files gauge'\'' ' # shellcheck disable=SC2016 echo ' echo "node_total_max_files_handles $fhmax"' # shellcheck disable=SC2016 echo ' echo "node_total_free_file_handles $fhfree"' # shellcheck disable=SC2016 echo ' echo "node_total_open_file_handles $fhopen"' echo '' echo ' }' echo '}' echo '' echo '################################' echo '### File Handler Connections ###' echo '################################' echo 'filehandler_connections() {' echo ' {' # shellcheck disable=SC2016,SC2028 echo ' fhconn=$(/usr/sbin/lsof -i | /usr/bin/awk '\''{gsub(/\\/,"_",$1)}1 {gsub(/\*|\:/"_",$2)}1 {gsub(/\*|\:|\\/,"",$9)}1'\'')' echo '' echo ' while read -r fhconn' echo ' do' # shellcheck disable=SC2016 echo ' conn=$(/usr/bin/awk '\''NR>1 {print "node_file_handle_connection{command=\""$1"\", connection=\""$9"\", user=\""$3"\", protocol=\""$5"\", type=\""$8"\"}", $2}'\'')' # shellcheck disable=SC2016 echo ' done <<< "$fhconn"' echo ' echo '\''# HELP node_file_handle_connection Connections by process.'\'' ' echo ' echo '\''# TYPE node_file_handle_connection gauge'\'' ' # shellcheck disable=SC2016 echo ' echo "$conn"' echo ' }' echo '}' echo '' echo '######################' echo '### Function Calls ###' echo '######################' echo 'processes_list' echo 'filehandlers_list' echo 'filehandler_connections' } > /usr/local/bin/cpu_usage.sh chmod 755 /usr/local/bin/cpu_usage.sh ################# ### SEARCH="java" ### for i in $(ps -C "${SEARCH}" -o pid | grep -v PID); do echo "PID # ${i} open files count : $(sudo ls -l /proc/${i}/fd | wc -l)"; done ############### ############### ### fuser -vm / 2>&1 | awk '$3 ~ /f|F/' | while read user pid flags rest; do printf '%10s %10s %10s %s\n' $user $pid $flags "$(