summaryrefslogtreecommitdiff
path: root/puppet/modules/site_check_mk/files/agent/local_checks/couchdb/leap_couch_stats.sh
blob: c7477b18d4b772d3a4539641074a165df3440339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
#
# todo:
#  - thresholds
#  - couch response time
#  - make CURL/URL/DBLIST_EXCLUDE vars configurable
#  - move load_nagios_utils() to helper library so we can use it from multiple scripts

start_time=$(date +%s.%N)

CURL='curl -s --netrc-file /etc/couchdb/couchdb.netrc'
URL='http://127.0.0.1:5984'
TMPFILE=$(mktemp)
DBLIST_EXCLUDE='(user-|sessions_|tokens_|_replicator|_users)'
PREFIX='Couchdb_'


load_nagios_utils () {
  # load the nagios utils
  # in debian, the package nagios-plugins-common installs utils.sh to /usr/lib/nagios/plugins/utils.sh
  utilsfn=
  for d in $PROGPATH /usr/lib/nagios/plugins /usr/lib64/nagios/plugins /usr/local/nagios/libexec /opt/nagios-plugins/libexec . ; do
    if [ -f "$d/utils.sh" ]; then
      utilsfn=$d/utils.sh;
    fi
  done
  if [ "$utilsfn" = "" ]; then
    echo "UNKNOWN - cannot find utils.sh (part of nagios plugins)";
    exit 3;
  fi
  . "$utilsfn";
  STATE[$STATE_OK]='OK'
  STATE[$STATE_WARNING]='Warning'
  STATE[$STATE_CRITICAL]='Critical'
  STATE[$STATE_UNKNOWN]='Unknown'
  STATE[$STATE_DEPENDENT]='Dependend'
}

get_global_stats_perf () {
  trap "localexit=3" ERR
  local localexit db_count
  localexit=0

  # get a list of all dbs
  $CURL -X GET $URL/_all_dbs | json_pp | egrep -v '(\[|\])' > $TMPFILE

  db_count=$( wc -l < $TMPFILE)
  excluded_db_count=$( egrep -c "$DBLIST_EXCLUDE" $TMPFILE )

  echo "db_count=$db_count|excluded_db_count=$excluded_db_count"
  return ${localexit}
}

db_stats () {
  trap "localexit=3" ERR
  local db db_stats doc_count del_doc_count localexit
  localexit=0

  db="$1"
  name="$2"

  if [ -z "$name" ]
  then
    name="$db"
  fi

  perf="$perf|${db}_docs=$( $CURL -s -X GET ${URL}/$db | json_pp |grep 'doc_count' | sed 's/[^0-9]//g' )"
  db_stats=$( $CURL -s -X GET ${URL}/$db | json_pp )

  doc_count=$( echo "$db_stats" | grep 'doc_count' | grep -v 'deleted_doc_count' | sed 's/[^0-9]//g' )
  del_doc_count=$( echo "$db_stats" | grep 'doc_del_count' | sed 's/[^0-9]//g' )

  # don't divide by zero
  if [ $del_doc_count -eq 0 ]
  then
    del_doc_perc=0
  else
    del_doc_perc=$(( del_doc_count * 100 / doc_count ))
  fi

  bytes=$( echo "$db_stats" | grep disk_size | sed 's/[^0-9]//g' )
  disk_size=$( echo "scale = 2; $bytes / 1024 / 1024" | bc -l )

  echo -n "${localexit} ${PREFIX}${name}_database ${name}_docs=$doc_count|${name}_deleted_docs=$del_doc_count|${name}_deleted_docs_percentage=${del_doc_perc}%"
  printf "|${name}_disksize_mb=%02.2fmb ${STATE[localexit]}: database $name\n" "$disk_size"

  return ${localexit}
}

# main

load_nagios_utils

# per-db stats
# get a list of all dbs
$CURL -X GET $URL/_all_dbs | json_pp | egrep -v '(\[|\])' > $TMPFILE

# get list of dbs to check
dbs=$( egrep -v "${DBLIST_EXCLUDE}" $TMPFILE | tr -d '\n"' | sed 's/,/ /g' )

for db in $dbs
do
  db_stats "$db"
done

# special handling for rotated dbs
suffix=$(($(date +'%s') / (60*60*24*30)))
db_stats "sessions_${suffix}" "sessions"
db_stats "tokens_${suffix}" "tokens"


# show global couchdb stats
global_stats_perf=$(get_global_stats_perf)
exitcode=$?

end_time=$(date +%s.%N)
duration=$( echo "scale = 2; $end_time - $start_time" | bc -l )

printf "${exitcode} ${PREFIX}global_stats ${global_stats_perf}|script_duration=%02.2fs ${STATE[exitcode]}: global couchdb status\n" "$duration"

rm "$TMPFILE"