| 1 | #!/bin/sh |
| 2 | |
| 3 | # ######################################################################## |
| 4 | # This program is part of $PROJECT_NAME$ |
| 5 | # License: GPL License (see COPYING) |
| 6 | # Authors: |
| 7 | # Baron Schwartz |
| 8 | # ######################################################################## |
| 9 | |
| 10 | # ######################################################################## |
| 11 | # Redirect STDERR to STDOUT; Nagios doesn't handle STDERR. |
| 12 | # ######################################################################## |
| 13 | exec 2>&1 |
| 14 | |
| 15 | # ######################################################################## |
| 16 | # Set up constants, etc. |
| 17 | # ######################################################################## |
| 18 | STATE_OK=0 |
| 19 | STATE_WARNING=1 |
| 20 | STATE_CRITICAL=2 |
| 21 | STATE_UNKNOWN=3 |
| 22 | STATE_DEPENDENT=4 |
| 23 | |
| 24 | # ######################################################################## |
| 25 | # Run the program. |
| 26 | # ######################################################################## |
| 27 | main() { |
| 28 | # Get options |
| 29 | for o; do |
| 30 | case "${o}" in |
| 31 | -c) shift; OPT_CRIT="${1}"; shift; ;; |
| 32 | --defaults-file) shift; OPT_DEFT="${1}"; shift; ;; |
| 33 | -g) shift; OPT_UNIX_GROUP="${1}"; shift; ;; |
| 34 | -H) shift; OPT_HOST="${1}"; shift; ;; |
| 35 | -l) shift; OPT_USER="${1}"; shift; ;; |
| 36 | -L) shift; OPT_LOPA="${1}"; shift; ;; |
| 37 | -p) shift; OPT_PASS="${1}"; shift; ;; |
| 38 | -P) shift; OPT_PORT="${1}"; shift; ;; |
| 39 | -S) shift; OPT_SOCK="${1}"; shift; ;; |
| 40 | -u) shift; OPT_UNIX_USER="${1}"; shift; ;; |
| 41 | -w) shift; OPT_WARN="${1}"; shift; ;; |
| 42 | --version) grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;; |
| 43 | --help) perl -00 -ne 'm/^ Usage:/ && print' "$0"; exit 0 ;; |
| 44 | -*) echo "Unknown option ${o}. Try --help."; exit 1; ;; |
| 45 | esac |
| 46 | done |
| 47 | OPT_UNIX_GROUP="${OPT_UNIX_GROUP:-mysql}" |
| 48 | OPT_UNIX_USER="${OPT_UNIX_USER:-mysql}" |
| 49 | if [ -e '/etc/nagios/mysql.cnf' ]; then |
| 50 | OPT_DEFT="${OPT_DEFT:-/etc/nagios/mysql.cnf}" |
| 51 | fi |
| 52 | if is_not_sourced; then |
| 53 | if [ -n "$1" ]; then |
| 54 | echo "WARN spurious command-line options: $@" |
| 55 | exit 1 |
| 56 | fi |
| 57 | fi |
| 58 | |
| 59 | # Set the exit status in case there are any problems. |
| 60 | NOTE="UNK could not determine the datadir location." |
| 61 | |
| 62 | # Set up files to hold one or more data directory locations. |
| 63 | local TEMP=$(mktemp -t "${0##*/}.XXXXXX") || exit $? |
| 64 | local DATADIRS=$(mktemp -t "${0##*/}.XXXXXX") || exit $? |
| 65 | trap "rm -f '${TEMP}' '${DATADIRS}' >/dev/null 2>&1" EXIT |
| 66 | |
| 67 | # If any connection option was given, then try to log in to find the datadir. |
| 68 | if [ "${OPT_DEFT}${OPT_HOST}${OPT_USER}${OPT_PASS}${OPT_PORT}${OPT_SOCK}" ]; then |
| 69 | # If this fails (e.g. we can't log in), then there will be no line in the |
| 70 | # file, and later we won't change the exit code / note away from "UNK". |
| 71 | mysql_exec "SELECT IF(@@datadir LIKE '/%', @@datadir, CONCAT(@@basedir, @@datadir))" >> "${DATADIRS}" |
| 72 | else |
| 73 | # Find all MySQL server instances. |
| 74 | for pid in $(_pidof mysqld); do |
| 75 | ps -p ${pid} -o pid,command | grep "${pid}" >> "${TEMP}" |
| 76 | done |
| 77 | # The ${TEMP} file may now contain lines like the following sample: |
| 78 | # 13822 /usr/sbin/mysqld --defaults-file=/var/lib/mysql/my.cnf \ |
| 79 | # --basedir=/usr --datadir=/var/lib/mysql/data/ \ |
| 80 | # --pid-file=/var/run/mysqld/mysqld.pid \ |
| 81 | # --socket=/var/run/mysqld/mysqld.sock |
| 82 | # Now the task is to read find any reference to a --datadir option. |
| 83 | # We store these into the $DATADIRS temp file. |
| 84 | # TODO: maybe in the future we can detect the user/group under which the |
| 85 | # process runs, and assume that is the right value, rather than defaulting |
| 86 | # to 'mysql'. |
| 87 | while read pid command; do |
| 88 | if echo "${command}" | grep datadir >/dev/null 2>&1; then |
| 89 | # Strip off everything up to and including --datadir= |
| 90 | command="${command##*--datadir=}" |
| 91 | # Strip off any options that follow this, assuming that there's not |
| 92 | # a space followed by a dash in the datadir's path. |
| 93 | echo "${command%% -*}" >> "${DATADIRS}" |
| 94 | fi |
| 95 | done < "${TEMP}" |
| 96 | fi |
| 97 | |
| 98 | WRONG="" |
| 99 | NOTE2="" |
| 100 | > ${TEMP} |
| 101 | while read datadir; do |
| 102 | FILES="$(find "${datadir}" \! -group "${OPT_UNIX_GROUP}" -o \! -user "${OPT_UNIX_USER}" 2>>${TEMP})" |
| 103 | if [ "${FILES}" ]; then |
| 104 | WRONG=1 |
| 105 | NOTE2="${NOTE2:+${NOTE2} }${FILES}" |
| 106 | fi |
| 107 | NOTE="OK all files/directories have correct ownership." |
| 108 | done < "${DATADIRS}" |
| 109 | |
| 110 | if [ -s "${TEMP}" ]; then |
| 111 | NOTE="UNK `cat ${TEMP}`" |
| 112 | elif [ "${WRONG}" ]; then |
| 113 | if [ "${OPT_CRIT}" ]; then |
| 114 | NOTE="CRIT files with wrong ownership: ${NOTE2}" |
| 115 | else |
| 116 | NOTE="WARN files with wrong ownership: ${NOTE2}" |
| 117 | fi |
| 118 | fi |
| 119 | |
| 120 | echo $NOTE |
| 121 | } |
| 122 | |
| 123 | # ######################################################################## |
| 124 | # Execute a MySQL command. |
| 125 | # ######################################################################## |
| 126 | mysql_exec() { |
| 127 | mysql ${OPT_DEFT:+--defaults-file="${OPT_DEFT}"} \ |
| 128 | ${OPT_LOPA:+--login-path="${OPT_LOPA}"} \ |
| 129 | ${OPT_HOST:+-h"${OPT_HOST}"} ${OPT_PORT:+-P"${OPT_PORT}"} \ |
| 130 | ${OPT_USER:+-u"${OPT_USER}"} ${OPT_PASS:+-p"${OPT_PASS}"} \ |
| 131 | ${OPT_SOCK:+-S"${OPT_SOCK}"} -ss -e "$1" |
| 132 | } |
| 133 | |
| 134 | # ######################################################################## |
| 135 | # A wrapper around pidof, which might not exist. The first argument is the |
| 136 | # command name to match. |
| 137 | # ######################################################################## |
| 138 | _pidof() { |
| 139 | if ! pidof "${1}" 2>/dev/null; then |
| 140 | ps axo pid,ucomm | awk -v comm="${1}" '$2 == comm { print $1 }' |
| 141 | fi |
| 142 | } |
| 143 | |
| 144 | # ######################################################################## |
| 145 | # Determine whether this program is being executed directly, or sourced/included |
| 146 | # from another file. |
| 147 | # ######################################################################## |
| 148 | is_not_sourced() { |
| 149 | [ "${0##*/}" = "pmp-check-mysql-file-privs" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ] |
| 150 | } |
| 151 | |
| 152 | # ######################################################################## |
| 153 | # Execute the program if it was not included from another file. |
| 154 | # This makes it possible to include without executing, and thus test. |
| 155 | # ######################################################################## |
| 156 | if is_not_sourced; then |
| 157 | OUTPUT=$(main "$@") |
| 158 | EXITSTATUS=$STATE_UNKNOWN |
| 159 | case "${OUTPUT}" in |
| 160 | UNK*) EXITSTATUS=$STATE_UNKNOWN; ;; |
| 161 | OK*) EXITSTATUS=$STATE_OK; ;; |
| 162 | WARN*) EXITSTATUS=$STATE_WARNING; ;; |
| 163 | CRIT*) EXITSTATUS=$STATE_CRITICAL; ;; |
| 164 | esac |
| 165 | echo "${OUTPUT}" |
| 166 | exit $EXITSTATUS |
| 167 | fi |
| 168 | |
| 169 | # ############################################################################ |
| 170 | # Documentation |
| 171 | # ############################################################################ |
| 172 | : <<'DOCUMENTATION' |
| 173 | =pod |
| 174 | |
| 175 | =head1 NAME |
| 176 | |
| 177 | pmp-check-mysql-file-privs - Alert if MySQL data directory privileges are wrong. |
| 178 | |
| 179 | =head1 SYNOPSIS |
| 180 | |
| 181 | Usage: pmp-check-mysql-file-privs [OPTIONS] |
| 182 | Options: |
| 183 | -c CRIT Critical threshold; makes a privilege issue critical. |
| 184 | --defaults-file FILE Only read mysql options from the given file. |
| 185 | Defaults to /etc/nagios/mysql.cnf if it exists. |
| 186 | -g GROUP The Unix group who should own the files; default mysql. |
| 187 | -H HOST MySQL hostname. |
| 188 | -l USER MySQL username. |
| 189 | -L LOGIN-PATH Use login-path to access MySQL (with MySQL client 5.6). |
| 190 | -p PASS MySQL password. |
| 191 | -P PORT MySQL port. |
| 192 | -S SOCKET MySQL socket file. |
| 193 | -u USER The Unix user who should own the files; default mysql. |
| 194 | -w WARN Warning threshold; ignored. |
| 195 | --help Print help and exit. |
| 196 | --version Print version and exit. |
| 197 | Options must be given as --option value, not --option=value or -Ovalue. |
| 198 | Use perldoc to read embedded documentation with more details. |
| 199 | |
| 200 | =head1 DESCRIPTION |
| 201 | |
| 202 | This Nagios plugin checks to make sure that the MySQL data directory, and its |
| 203 | contents, is owned by the correct Unix user and group. If the ownership is |
| 204 | incorrect, then the server might fail due to lack of permission to modify its |
| 205 | data. For example, suppose a system administrator enters a database directory |
| 206 | and creates a file that is owned by root. Now a database administrator issues a |
| 207 | DROP TABLE command, which fails because it is unable to remove the file and thus |
| 208 | the non-empty directory cannot be removed either. |
| 209 | |
| 210 | The plugin accepts the -g and -u options to specify which Unix user and group |
| 211 | should own the data directory and its contents. This is usually the user account |
| 212 | under which MySQL runs, which is mysql by default on most systems. The plugin |
| 213 | assumes that user and group by default, too. |
| 214 | |
| 215 | The plugin accepts the -w and -c options for compatibility with standard Nagios |
| 216 | plugin conventions, but they are not based on a threshold. Instead, the plugin |
| 217 | raises a warning by default, and if the -c option is given, it raises an error |
| 218 | instead, regardless of the option's value. |
| 219 | |
| 220 | By default, this plugin will attempt to detect all running instances of MySQL, |
| 221 | and verify the data directory ownership for each one. It does this purely by |
| 222 | examining the Unix process table with the C<ps> tool. However, in some cases |
| 223 | the process's command line does not list the path to the data directory. If the |
| 224 | tool fails to detect the MySQL server process, or if you wish to limit the check |
| 225 | to a single instance in the event that there are multiple instances on a single |
| 226 | server, then you can specify MySQL authentication options. This will cause the |
| 227 | plugin to skip examining the Unix processlist, log into MySQL, and examine the |
| 228 | datadir variable from SHOW VARIABLES to find the location of the data directory. |
| 229 | |
| 230 | In case an user you are calling this plugin from has no permissions to examine |
| 231 | the datadir the plugin raises an unknown with the explanation. |
| 232 | |
| 233 | =head1 PRIVILEGES |
| 234 | |
| 235 | This plugin executes the following commands against MySQL: |
| 236 | |
| 237 | =over |
| 238 | |
| 239 | =item * |
| 240 | |
| 241 | C<SELECT> the MySQL system variables C<@@datadir> and C<@@basedir>. |
| 242 | |
| 243 | =back |
| 244 | |
| 245 | This plugin executes the following UNIX commands that may need special privileges: |
| 246 | |
| 247 | =over |
| 248 | |
| 249 | =item * |
| 250 | |
| 251 | ps |
| 252 | |
| 253 | =item * |
| 254 | |
| 255 | find C<datadir> |
| 256 | |
| 257 | =back |
| 258 | |
| 259 | The plugin should be able to either get variables from MySQL or find mysqld |
| 260 | PID using C<ps> command. |
| 261 | |
| 262 | On BSD, if C<sysctl> option C<security.bsd.see_other_uids> is set to 0, C<ps> |
| 263 | will not return mysqld PID if the plugin run from non-root user. |
| 264 | |
| 265 | Also an user you run the plugin from should be able to access MySQL datadir |
| 266 | files, so you may want to add it into mysql unix group etc. |
| 267 | |
| 268 | =head1 COPYRIGHT, LICENSE, AND WARRANTY |
| 269 | |
| 270 | This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc. |
| 271 | Feedback and improvements are welcome. |
| 272 | |
| 273 | THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 274 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 275 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 276 | |
| 277 | This program is free software; you can redistribute it and/or modify it under |
| 278 | the terms of the GNU General Public License as published by the Free Software |
| 279 | Foundation, version 2. You should have received a copy of the GNU General |
| 280 | Public License along with this program; if not, write to the Free Software |
| 281 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 282 | |
| 283 | =head1 VERSION |
| 284 | |
| 285 | $PROJECT_NAME$ pmp-check-mysql-file-privs $VERSION$ |
| 286 | |
| 287 | =cut |
| 288 | |
| 289 | DOCUMENTATION |