Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / other / mysql_report @ 23f17f67

Historique | Voir | Annoter | Télécharger (4,67 ko)

1
#!/bin/sh
2
#
3
# Plugin to graph numeric result of the specified MySQL queries.
4
#
5
# Parameters:
6
#
7
#       config   (required)
8
#       autoconf (optional - used by lrrd-config)
9
#
10
# Configuration example
11
#
12
# [mysql_report]
13
# env.names query1 query2
14
# env.hostname www.example.com
15
# env.database mysql
16
# env.username report
17
# env.password secret
18
# env.errorvalue 60
19
# env.max 120
20
#
21
# env.label_query1 Example query#1 (number of users)
22
# env.hostname_query1 www1.example.com
23
# env.draw_query1 AREA
24
# env.colour_query1 FF0000
25
# env.database_query1 mysql
26
# env.username_query1 user1
27
# env.password_query1 secret1
28
# env.query_query1 select count(*) from `exampledb`.`users`
29
# env.warning_query1 5
30
# env.critical_query1 8
31
#
32
# env.label_query2 Example query#2 (number of successful logins)
33
# env.query_query2 select count(*) from `exampledb`.`login_log`
34
# env.type_query2 DERIVE
35
# env.draw_query2 LINE2
36
# env.colour_query2 000080
37
# env.mysqlopts_query2 --defaults-extra-file=/etc/mysql/debian.cnf --port=4507
38
#
39
# env.cdefnames cdef1
40
# env.cdef_cdef1 result1,result2,+
41
# env.cdeflbl_cdef1 Users and successful logins sum
42

    
43
#
44
# $Log$
45
#
46
# Revision 1.0  2010/04/19 14:18:32 muzso@muzso.hu
47
# Initial version
48
#
49
# Revision 1.01 2010/7/14 oded@pageonce.com
50
#%# family=auto
51
#%# capabilities=autoconf
52

    
53
mysqlbin=$(which mysql)
54

    
55
default_errorvalue=30
56
default_title="Results from MySQL queries"
57
default_category="mysql"
58
default_vlabel="value / sec"
59
default_info="This graph shows results of one or more SQL queries."
60
default_args="--base 1000 -l 0"
61
default_scale="no"
62

    
63
if [ "${1}" = "autoconf" ]; then
64
  result=0
65
  if [ -z "${mysqlbin}" ]; then
66
    echo "no"
67
  else
68
    echo "yes"
69
  fi
70
  exit $result
71
fi
72

    
73
if [ -z "${names}" ]; then
74
  echo "Configuration required"
75
  exit 1
76
fi
77

    
78
[ -n "${errorvalue}" ] || errorvalue=${default_errorvalue}
79
[ -n "${title}" ] || title="${default_title}"
80
[ -n "${category}" ] || category="${default_category}"
81
[ -n "${vlabel}" ] || vlabel="${default_vlabel}"
82
[ -n "${info}" ] || info="${default_info}"
83
[ -n "${args}" ] || args="${default_args}"
84
[ -n "${scale}" ] || scale="${default_scale}"
85

    
86
if [ "${1}" = "config" ]; then
87
  cat << EOH1
88
graph_title ${title}
89
graph_args ${args}
90
graph_scale ${scale}
91
graph_vlabel ${vlabel}
92
graph_category ${category}
93
graph_info ${info}
94
EOH1
95
  [[ -n "${period}" ]] && echo "graph_period ${period}"
96
  I=1
97
  for name in ${names}; do
98
    eval iquery='${query_'${name}'}'
99
    if [ -n "${iquery}" ]; then
100
      eval ilabel='${label_'${name}':-host${I}}'
101
      eval iwarning='${warning_'${name}':-${warning}}'
102
      eval icritical='${critical_'${name}':-${critical}}'
103
      eval imax='${max_'${name}':-${max}}'
104
      eval itype='${type_'${name}':-${type}}'
105
      eval idraw='${draw_'${name}':-${draw}}'
106
      eval icolour='${colour_'${name}':-${colour}}'
107
      iquery=$(echo "${iquery}" | tr '\n\r' '  ')
108
      cat << EOH2
109
result${I}.label ${ilabel}
110
result${I}.info Result of the query: ${iquery}
111
result${I}.min 0
112
EOH2
113
      [ -n "${imax}" ] && echo "result${I}.max ${imax}"
114
      [ -n "${itype}" ] && echo "result${I}.type ${itype}"
115
      [ -n "${idraw}" ] && echo "result${I}.draw ${idraw}"
116
      [ -n "${icolour}" ] && echo "result${I}.colour ${icolour}"
117
      [ -n "${iwarning}" ] && echo "result${I}.warning ${iwarning}"
118
      [ -n "${icritical}" ] && echo "result${I}.critical ${icritical}"
119
      I=$((I+1))
120
    fi
121
  done
122
  for cdefname in ${cdefnames} ;do
123
    eval icdef='${cdef_'${cdefname}'}'
124
    if [ -n "${icdef}" ]; then
125
      eval icdeflbl='${cdeflbl_'${cdefname}':-${cdeflbl}}'
126
      cat << EOH4
127
${cdefname}.cdef ${icdef}
128
${cdefname}.label ${icdeflbl}
129
EOH4
130
    fi
131
  done
132
  exit 0
133
fi
134

    
135
I=1
136
for name in ${names}; do
137
  eval iquery='${query_'${name}'}'
138
  if [ -n "${iquery}" ]; then
139
		eval ihostname='${hostname_'${name}':-${hostname}}'
140
		[ -n "${ihostname}" ] && opts="${opts} --host=${ihostname}"
141
		eval idatabase='${database_'${name}':-${database}}'
142
		eval iusername='${username_'${name}':-${username}}'
143
		[ -n "${iusername}" ] && opts="${opts} --user=${iusername}"
144
    eval ipassword='${password_'${name}':-${password}}'
145
    [ -n "${ipassword}" ] && opts="${opts} --password=${ipassword}"
146
    eval ierrorvalue='${errorvalue_'${name}':-${errorvalue}}'
147
    eval imysqlopts='${mysqlopts_'${name}':-${mysqlopts}}'
148
    iquery=$(echo "${iquery}" | tr '\n\r' '  ')
149
    #echo "The command to be executed: echo \"${iquery}\" | ${mysqlbin} ${imysqlopts} --batch --skip-column-names ${opts} ${idatabase} 2>&1"
150
    output=$(echo "${iquery}" | ${mysqlbin} ${imysqlopts} --batch --skip-column-names ${opts} ${idatabase} 2>&1)
151
    if [ $? -ne 0 -a ${ierrorvalue} -ne 0 ]; then
152
      result=${ierrorvalue}
153
    else
154
      result=$(echo "${output}" | head -n 1 | sed 's/^\([0-9]\+\).*/\1/')
155
    fi
156
    echo "result${I}.value ${result}"
157
    I=$((I+1))
158
  fi
159
done
160

    
161