root / plugins / mysql / mysql_report @ 17f78427
Historique | Voir | Annoter | Télécharger (4,58 ko)
| 1 | ce0d15ac | M?ller Zsolt | #!/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 | 23f17f67 | Oded Ben Ozer | # env.draw_query1 AREA |
| 24 | # env.colour_query1 FF0000 |
||
| 25 | ce0d15ac | M?ller Zsolt | # 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 | 23f17f67 | Oded Ben Ozer | # env.draw_query2 LINE2 |
| 36 | # env.colour_query2 000080 |
||
| 37 | ce0d15ac | M?ller Zsolt | # env.mysqlopts_query2 --defaults-extra-file=/etc/mysql/debian.cnf --port=4507 |
| 38 | # |
||
| 39 | 23f17f67 | Oded Ben Ozer | # env.cdefnames cdef1 |
| 40 | # env.cdef_cdef1 result1,result2,+ |
||
| 41 | # env.cdeflbl_cdef1 Users and successful logins sum |
||
| 42 | |||
| 43 | # |
||
| 44 | ce0d15ac | M?ller Zsolt | # $Log$ |
| 45 | # |
||
| 46 | # Revision 1.0 2010/04/19 14:18:32 muzso@muzso.hu |
||
| 47 | # Initial version |
||
| 48 | # |
||
| 49 | 23f17f67 | Oded Ben Ozer | # Revision 1.01 2010/7/14 oded@pageonce.com |
| 50 | ce0d15ac | M?ller Zsolt | #%# family=auto |
| 51 | #%# capabilities=autoconf |
||
| 52 | |||
| 53 | mysqlbin=$(which mysql) |
||
| 54 | |||
| 55 | default_errorvalue=30 |
||
| 56 | default_title="Results from MySQL queries" |
||
| 57 | default_vlabel="value / sec" |
||
| 58 | default_info="This graph shows results of one or more SQL queries." |
||
| 59 | default_args="--base 1000 -l 0" |
||
| 60 | default_scale="no" |
||
| 61 | |||
| 62 | if [ "${1}" = "autoconf" ]; then
|
||
| 63 | result=0 |
||
| 64 | if [ -z "${mysqlbin}" ]; then
|
||
| 65 | echo "no" |
||
| 66 | else |
||
| 67 | echo "yes" |
||
| 68 | fi |
||
| 69 | exit $result |
||
| 70 | fi |
||
| 71 | |||
| 72 | if [ -z "${names}" ]; then
|
||
| 73 | echo "Configuration required" |
||
| 74 | exit 1 |
||
| 75 | fi |
||
| 76 | |||
| 77 | [ -n "${errorvalue}" ] || errorvalue=${default_errorvalue}
|
||
| 78 | [ -n "${title}" ] || title="${default_title}"
|
||
| 79 | [ -n "${vlabel}" ] || vlabel="${default_vlabel}"
|
||
| 80 | [ -n "${info}" ] || info="${default_info}"
|
||
| 81 | [ -n "${args}" ] || args="${default_args}"
|
||
| 82 | [ -n "${scale}" ] || scale="${default_scale}"
|
||
| 83 | |||
| 84 | if [ "${1}" = "config" ]; then
|
||
| 85 | cat << EOH1 |
||
| 86 | graph_title ${title}
|
||
| 87 | graph_args ${args}
|
||
| 88 | graph_scale ${scale}
|
||
| 89 | graph_vlabel ${vlabel}
|
||
| 90 | 8af93fce | dipohl | graph_category db |
| 91 | ce0d15ac | M?ller Zsolt | graph_info ${info}
|
| 92 | EOH1 |
||
| 93 | 96f2bc00 | Stig Sandbeck Mathisen | [ -n "${period}" ] && echo "graph_period ${period}"
|
| 94 | ce0d15ac | M?ller Zsolt | I=1 |
| 95 | for name in ${names}; do
|
||
| 96 | eval iquery='${query_'${name}'}'
|
||
| 97 | if [ -n "${iquery}" ]; then
|
||
| 98 | eval ilabel='${label_'${name}':-host${I}}'
|
||
| 99 | eval iwarning='${warning_'${name}':-${warning}}'
|
||
| 100 | eval icritical='${critical_'${name}':-${critical}}'
|
||
| 101 | eval imax='${max_'${name}':-${max}}'
|
||
| 102 | eval itype='${type_'${name}':-${type}}'
|
||
| 103 | 5df115fa | Oded Ben Ozer | eval idraw='${draw_'${name}':-${draw}}'
|
| 104 | eval icolour='${colour_'${name}':-${colour}}'
|
||
| 105 | ce0d15ac | M?ller Zsolt | iquery=$(echo "${iquery}" | tr '\n\r' ' ')
|
| 106 | cat << EOH2 |
||
| 107 | result${I}.label ${ilabel}
|
||
| 108 | result${I}.info Result of the query: ${iquery}
|
||
| 109 | result${I}.min 0
|
||
| 110 | EOH2 |
||
| 111 | [ -n "${imax}" ] && echo "result${I}.max ${imax}"
|
||
| 112 | [ -n "${itype}" ] && echo "result${I}.type ${itype}"
|
||
| 113 | 5df115fa | Oded Ben Ozer | [ -n "${idraw}" ] && echo "result${I}.draw ${idraw}"
|
| 114 | [ -n "${icolour}" ] && echo "result${I}.colour ${icolour}"
|
||
| 115 | ce0d15ac | M?ller Zsolt | [ -n "${iwarning}" ] && echo "result${I}.warning ${iwarning}"
|
| 116 | [ -n "${icritical}" ] && echo "result${I}.critical ${icritical}"
|
||
| 117 | I=$((I+1)) |
||
| 118 | fi |
||
| 119 | done |
||
| 120 | 5df115fa | Oded Ben Ozer | for cdefname in ${cdefnames} ;do
|
| 121 | eval icdef='${cdef_'${cdefname}'}'
|
||
| 122 | if [ -n "${icdef}" ]; then
|
||
| 123 | eval icdeflbl='${cdeflbl_'${cdefname}':-${cdeflbl}}'
|
||
| 124 | cat << EOH4 |
||
| 125 | ${cdefname}.cdef ${icdef}
|
||
| 126 | ${cdefname}.label ${icdeflbl}
|
||
| 127 | EOH4 |
||
| 128 | fi |
||
| 129 | done |
||
| 130 | ce0d15ac | M?ller Zsolt | exit 0 |
| 131 | fi |
||
| 132 | |||
| 133 | I=1 |
||
| 134 | for name in ${names}; do
|
||
| 135 | eval iquery='${query_'${name}'}'
|
||
| 136 | if [ -n "${iquery}" ]; then
|
||
| 137 | eval ihostname='${hostname_'${name}':-${hostname}}'
|
||
| 138 | [ -n "${ihostname}" ] && opts="${opts} --host=${ihostname}"
|
||
| 139 | eval idatabase='${database_'${name}':-${database}}'
|
||
| 140 | eval iusername='${username_'${name}':-${username}}'
|
||
| 141 | [ -n "${iusername}" ] && opts="${opts} --user=${iusername}"
|
||
| 142 | eval ipassword='${password_'${name}':-${password}}'
|
||
| 143 | [ -n "${ipassword}" ] && opts="${opts} --password=${ipassword}"
|
||
| 144 | eval ierrorvalue='${errorvalue_'${name}':-${errorvalue}}'
|
||
| 145 | eval imysqlopts='${mysqlopts_'${name}':-${mysqlopts}}'
|
||
| 146 | iquery=$(echo "${iquery}" | tr '\n\r' ' ')
|
||
| 147 | #echo "The command to be executed: echo \"${iquery}\" | ${mysqlbin} ${imysqlopts} --batch --skip-column-names ${opts} ${idatabase} 2>&1"
|
||
| 148 | output=$(echo "${iquery}" | ${mysqlbin} ${imysqlopts} --batch --skip-column-names ${opts} ${idatabase} 2>&1)
|
||
| 149 | if [ $? -ne 0 -a ${ierrorvalue} -ne 0 ]; then
|
||
| 150 | result=${ierrorvalue}
|
||
| 151 | else |
||
| 152 | result=$(echo "${output}" | head -n 1 | sed 's/^\([0-9]\+\).*/\1/')
|
||
| 153 | fi |
||
| 154 | echo "result${I}.value ${result}"
|
||
| 155 | I=$((I+1)) |
||
| 156 | fi |
||
| 157 | done |
||
| 158 |
