1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 14:16:00 +00:00

code rewrite

removed bashisms and cleaned everything up
This commit is contained in:
Jonas Palm 2016-10-24 23:03:18 +02:00 committed by GitHub
parent feb54309ca
commit c679de12ce

View file

@ -1,10 +1,13 @@
#!/bin/bash #!/usr/bin/env sh
# wordpress-multisite plugin # wordpress-multisite plugin
# #
# Author Jonas Palm # Version 0.2
# E-Mail jonaspalm . posteo. de # Date 2016-10-24
# Code improvements
#
# Version 0.1 # Version 0.1
# Date 2016-02-07 # Date 2016-02-07
# Initial release
# #
: <<=cut : <<=cut
=head1 NAME =head1 NAME
@ -21,108 +24,98 @@ The plugin need access to the wordpress database
=head2 Config file =head2 Config file
Create the config file plugin-conf.d/wordpress with the following values: Create the config file plugin-conf.d/wordpress with the following values:
=over 6 =over 4
=item * [wordpress*] =item * [wordpress*]
=item * env.DB_USER <database user (mandatory)> =item * env.mysqlopts <ie -uroot -prootpass>
=item * env.DB_PASSWORD <database password (mandatory)> =item * env.mysqlconnection <defaults to -hlocalhost>
=item * env.DB_NAME <database name (defaults to wordpress> =item * env.database <ie -Dwordpress>
=item * env.DB_PREFIX <database prefix (defaults to wp_)> =item * env.dbprefix <defaults to wp_>
=item * env.DB_HOST <database host (defaults to localhost)>
=item * env.DB_PORT <database port (defaults to 3306)>
=back =back
=head1 VERSION =head1 VERSION
Version 0.2 (2016-02-07)
0.1 2016-02-07
=head1 AUTHOR =head1 AUTHOR
Jonas Palm <jonaspalm . posteo . de> Jonas Palm <jonaspalm . posteo . de>
=head1 LICENSE
GPLv3 or higher
=cut =cut
# Fill some variables # fill vars
DB_USER=${DB_USER} DB_OPTIONS=${mysqlopts}
DB_PASSWORD=${DB_PASSWORD} DB_CONNECTION=${mysqlconnection:--hlocalhost}
DB_NAME=${DB_NAME:-wordpress} DB_NAME=${database}
DB_PREFIX=${DB_PREFIX:-wp_} DB_PREFIX=${dbprefix:-wp_}
DB_HOST=${DB_HOST:-localhost}
DB_PORT=${DB_PORT:-3306}
MYSQLOPTS="-h$DB_HOST -P $DB_PORT -p$DB_PASSWORD -u$DB_USER -D $DB_NAME --column-names=0 -s" MYSQL_CMD=$(which mysql)
wp_get() {
case $1 in
comments) QUERY="SELECT COUNT(*) FROM ${DB_PREFIX}${2}comments WHERE comment_approved = '1' AND comment_type = '';" ;;
ids) QUERY="SELECT blog_id FROM ${DB_PREFIX}blogs;" ;;
pingbacks) QUERY="SELECT COUNT(*) FROM ${DB_PREFIX}${2}comments WHERE comment_approved = '1' AND comment_type = 'pingback';" ;;
posts) QUERY="SELECT COUNT(*) FROM ${DB_PREFIX}${2}posts WHERE post_status = 'publish' AND post_password = '' AND post_type = 'post';" ;;
title) QUERY="SELECT option_value FROM ${DB_PREFIX}${2}options WHERE option_name = 'siteurl';" ;;
users) QUERY="SELECT COUNT(*) FROM ${DB_PREFIX}users;"
esac
$MYSQL_CMD $DB_CONNECTION $DB_OPTIONS $DB_NAME --column-names=0 -s --execute="$QUERY"
}
# whole network
if [ "$1" = "config" ]; then if [ "$1" = "config" ]; then
echo "multigraph wordpress" echo "multigraph wordpress"
echo "graph_title Wordpress Mulitsite" echo "graph_title Wordpress Mulitsite"
echo "graph_order instances users posts comments pingbacks" echo "graph_order instances users posts comments pingbacks"
echo "graph_vlabel Wordpress" echo "graph_vlabel Wordpress"
echo "graph_category Wordpress" echo "graph_category Wordpress"
echo "graph_info Some Statistics of Wordpress" echo "graph_info Some Statistics of Wordpress"
echo "instances.label Instances" echo "instances.label Instances"
echo "users.label Users" echo "users.label Users"
echo "posts.label Posts" echo "posts.label Posts"
echo "comments.label Comments" echo "comments.label Comments"
echo "pingbacks.label Pingbacks" echo "pingbacks.label Pingbacks"
else else
CNT=0 for n in $(wp_get ids); do
for n in `mysql $MYSQLOPTS --execute="select blog_id from ${DB_PREFIX}blogs"`; do i=
if [ "$n" == "1" ]; then test "$n" -gt "1" && i=${n}_
i=
else
i=${n}_
fi
POSTS=`mysql $MYSQLOPTS --execute="SELECT COUNT(*) FROM ${DB_PREFIX}${i}posts WHERE post_status = 'publish' AND post_password = '' AND post_type = 'post';"` POSTS=$(expr $POSTS + $(wp_get posts $i))
(( POSTS_ += POSTS )) COMMENTS=$(expr $COMMENTS + $(wp_get comments $i))
PINGBACKS=$(expr $PINGBACKS + $(wp_get pingbacks $i))
CNT=$(expr $CNT + 1)
done
COMMENTS=`mysql $MYSQLOPTS --execute="SELECT COUNT(*) FROM ${DB_PREFIX}${i}comments WHERE comment_approved = '1' AND comment_type = '';"` echo "multigraph wordpress"
(( COMMENTS_ += COMMENTS )) echo "posts.value $POSTS"
echo "comments.value $COMMENTS"
PINGBACKS=`mysql $MYSQLOPTS --execute="SELECT COUNT(*) FROM ${DB_PREFIX}${i}comments WHERE comment_approved = '1' AND comment_type = 'pingback';"` echo "pingbacks.value $PINGBACKS"
(( PINGBACKS_ += PINGBACKS )) echo "instances.value $CNT"
echo "users.value $(wp_get users)"
(( CNT += 1 ))
done
# return values
echo "multigraph wordpress"
echo "posts.value $POSTS_"
echo "comments.value $COMMENTS_"
echo "pingbacks.value $PINGBACKS_"
echo "instances.value $CNT"
echo "users.value `mysql $MYSQLOPTS --execute="SELECT COUNT(*) FROM ${DB_PREFIX}users ;"`"
fi fi
# single blogs # single blogs
for n in `mysql $MYSQLOPTS --execute="select blog_id from ${DB_PREFIX}blogs"`; do for n in $(wp_get ids); do
if [ "${n}" == "1" ]; then i=
i= test "$n" -gt "1" && i=${n}_
else test "$n" -le "9" && n=0${n}
i=${n}_
fi
if [ "$n" -le "9" ]; then if [ "$1" = "config" ]; then
n=0${n} echo "multigraph wordpress.site_${n}"
fi echo "graph_title $(wp_get title $i)"
echo "graph_order posts comments pingbacks"
if [ "$1" = "config" ]; then echo "graph_vlabel Wordpress ID ${n}"
echo "multigraph wordpress.site_${n}" echo "posts.label Posts"
echo "graph_title `mysql $MYSQLOPTS --execute=\"select option_value from ${DB_PREFIX}${i}options where option_name = 'siteurl';\"`" echo "comments.label Comments"
echo "graph_order posts comments pingbacks" echo "pingbacks.label Pingbacks"
echo "graph_vlabel Wordpress ID ${n}" else
echo "posts.label Posts" echo "multigraph wordpress.site_${n}"
echo "comments.label Comments" echo "posts.value $(wp_get posts $i)"
echo "pingbacks.label Pingbacks" echo "comments.value $(wp_get comments $i)"
else echo "pingbacks.value $(wp_get pingbacks $i)"
POSTS=`mysql $MYSQLOPTS --execute="SELECT COUNT(*) FROM ${DB_PREFIX}${i}posts WHERE post_status = 'publish' AND post_password = '' AND post_type = 'post';"` fi
COMMENTS=`mysql $MYSQLOPTS --execute="SELECT COUNT(*) FROM ${DB_PREFIX}${i}comments WHERE comment_approved = '1' AND comment_type = '';"`
PINGBACKS=`mysql $MYSQLOPTS --execute="SELECT COUNT(*) FROM ${DB_PREFIX}${i}comments WHERE comment_approved = '1' AND comment_type = 'pingback';"`
# return values
echo "multigraph wordpress.site_${n}"
echo "posts.value $POSTS"
echo "comments.value $COMMENTS"
echo "pingbacks.value $PINGBACKS"
fi
done done