Linux monitoring

From Essential
Revision as of 00:25, 6 November 2016 by Tcepo (talk | contribs) (Created page with "== Writting activity == === bash mylogs -ie "critical\|error\|\warn|\fail" === <pre> MyBin="mylogs" >~/$MyBin chmod 755 ~/$MyBin cat <<EOT >>~/$MyBin #!/bin/bash MyKey="\$@"...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Writting activity

bash mylogs -ie "critical\|error\|\warn|\fail"

MyBin="mylogs"

>~/$MyBin
chmod 755 ~/$MyBin
cat <<EOT >>~/$MyBin
#!/bin/bash

MyKey="\$@"
MyBin="\$(basename \$0)"

MyTimeOutMin=900
MyTimeOutMax=3600

# bash colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

lsof -X 2>/dev/null |grep -E " [0-9]+w " |grep " REG " |grep -o -P "(\/.+)" |sort -g |uniq |grep -v "/\${MyBin}.cfg\$" >~/\${MyBin}.cfg

MyCacheFile="/tmp/\${MyBin}.\$\$"

function MyStep1 {
>\${MyCacheFile}

# 3 derniers jours du mois ou 9h à 17h en jour ouvré
if [ \$(date +%d -d "3 day") -lt \$(date +%d) ] || { [ \$(date +%H) -ge 9 ] && [ \$(date +%H) -lt 17 ] && [ \$(date +%u) -lt 6 ] ;} ;then
  MyTimeOut=\$MyTimeOutMin
else
  MyTimeOut=\$MyTimeOutMax
fi

cat ~/\${MyBin}.cfg |while read MyLogPath ;do
  if [ -f "\$MyLogPath" ] ;then
    MyLogElapsed=\$((\$(date +%s) - \$(date +%s -r "\$MyLogPath")))

    if [ "\$MyLogElapsed" -lt "\$MyTimeOut" ] ;then
      MyResult="\$(tail -666 "\$MyLogPath" 2>/dev/null |strings |grep "\$MyKey" |tail -2 |cut -c1-111)"
      if [ -z "\$MyResult" ] ;then
        MyResult="\$(tail -2 "\$MyLogPath" 2>/dev/null |strings |tail -2 |cut -c1-111)"
        if [ ! -z "\$MyResult" ] ;then
          echo -e "\${NC}== \$MyLogPath ==" >>\${MyCacheFile}
          echo -e "\${GREEN}\${MyResult}\${NC}" >>\${MyCacheFile}
        fi
      else
        echo -e "\${NC}== \$MyLogPath ==" >>\${MyCacheFile}
        echo -e "\${RED}\${MyResult}\${NC}" >>\${MyCacheFile}
      fi
    fi
  fi
done
}

MyStep1
cat \${MyCacheFile} |grep -v -w -f ~/\${MyBin}.cfg >~/\${MyBin}.ref

while true ; do
  MyStep1
  diff --changed-group-format='%>' --unchanged-group-format='' ~/\${MyBin}.ref \${MyCacheFile} >\${MyCacheFile}.diff
  clear
  cat \${MyCacheFile}.diff
  sleep 2
done
EOT

kill -9 $$
~/mylogs -ie "critical\|error\|\warn|\fail"