File: munin-check

package info (click to toggle)
munin 2.999.14-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 10,036 kB
  • sloc: perl: 31,052; sh: 12,473; java: 1,924; python: 852; makefile: 336; ruby: 232; awk: 169; xml: 45; sql: 11
file content (189 lines) | stat: -rwxr-xr-x 5,067 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
# Copyright (C) 2008 Matthias Schmitz
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#


####
# prints usage
function usage() {
    echo "Usage: munin-check [options]
Options:
   -h|--help             Show this help.
   -v|--verbose          Be verbose.
   -f|--fix-permissions  Fix the permissions of the munin dirs and files.
                         Needs superuser rights.

Please don't use this script if you are using 'graph_strategy cgi'!
It doesn't care about the right permissions for www-data yet...
"
}

# Get options from the command line
TEMP=$(getopt -o fhv --long fix-permissions,help,verbose -n 'munin-check' -- "$@")

# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
    echo "Terminating..." >&2
    exit 1
fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

PLEASE_FIXME="false"
VERBOSE="false"
while :; do
    case "$1" in
        -h|--help) usage ; exit 0; shift ;;
        -v|--verbose) VERBOSE="true"; shift;;
        -f|--fix-permissions) PLEASE_FIXME="true" ;  shift ;;
        --) shift ; break ;;
        *) echo "Internal error!" ; exit 1 ;;
    esac
done


function msg_info() {
    local message="$1"
    if [ "$VERBOSE" = "true" ]; then
        echo "$message"
    fi
}


####
# sets owner to "@@USER@@"
function fix_owner() {
    fix_object=$1; shift
    fix_owner=$1; shift

    if [[ $(id -u) -eq 0 ]]; then
        msg_info "# $fix_object : Fixing owner ($fix_owner)"
        # -R is wrong, in case we're not recursing, and if we are
        # recursing then fix_owner will be called again.  OK, that's
        # slower, but still more correct.
        chown "$fix_owner" "$fix_object"
    else
        echo "Fixing the permissions needs superuser rights. You should run \"munin-check -f\" as root."
        exit 0
    fi
}


####
# check if "@@USER@@" is owner, if PLEASE_FIXME set it calls fix_owner()
function owner_ok() {
    object=$1; shift || exit 1
    correctowner=$1; shift || exit 1

    owner=$(stat --format %U "$object")
    if [[ "$owner" != "$correctowner" ]]; then
        echo "# $object : Wrong owner ($owner != $correctowner)"
        if [[ "$PLEASE_FIXME" == "true" ]]; then
            fix_owner "$object" "$correctowner"
        fi
    fi

    if [[ -d $object ]]; then
        if [[ -n "$norec" ]]; then
            # The $norec variable cuts off recursion
            return 0
        fi

        case $object in
            "lost+found") return 0;;
        esac

        # ... and then dive into it
        for subobject in "$object"/*; do
            owner_ok "$subobject" "$correctowner"
        done
    fi
}


function perm_ok(){
    object=$1; shift || exit 1
    correctperm=$1; shift || exit 1

    perm=$(perl -e 'printf "%o\n", 07777 & (stat $ARGV[0])[2]' "$object")
    if [[ $perm -ne $correctperm ]]; then
        echo "# $object : Wrong permissions ($perm != $correctperm)"
        if [[ "$PLEASE_FIXME" == "true" ]]; then
            msg_info "# $object : Fixing permission ($correctperm)"
            chmod "$correctperm" "$object"
        fi
    fi

    if [[ -d $object ]]; then
        # check the owner of the dir ...

        if [[ -n "$norec" ]]; then
            # The $norec variable cuts off recursion
            return 0
        fi

        case $object in
            "lost+found") return 0;;
        esac

        # ... and then dive into it
        for subobject in "$object"/*; do
            perm_ok "$subobject" "$correctperm"
        done
    fi
}


####
# main

# allow recursion by default (can be overridden below for each function call)
norec=
echo "check @@HTMLDIR@@"
owner_ok "@@HTMLDIR@@" "@@USER@@"

for dir in "@@DBDIR@@"/*; do
    # Do not check the plugin-state directory
    case $dir in
        */plugin-state)
            continue;;
    esac
    echo "check $dir"
    owner_ok "$dir" "@@USER@@"
done

echo "check miscellaneous"
norec=yes owner_ok @@LOGDIR@@ @@USER@@

norec=yes owner_ok "@@DBDIR@@" "@@USER@@"
norec=yes perm_ok "@@DBDIR@@" 755

for dir in "@@DBDIR@@/datafile" "@@DBDIR@@/limits" "@@DBDIR@@"/*.stats; do
    norec=yes owner_ok "$dir" "@@USER@@"
    norec=yes perm_ok "$dir" 644
done

norec=yes owner_ok "@@PLUGSTATE@@" "@@PLUGINUSER@@"
norec=yes perm_ok "@@PLUGSTATE@@" 775

norec=yes perm_ok "@@CONFDIR@@/plugin-conf.d" 755

echo "Check done.  Please note that this script only checks most things,"
echo "not all things."
echo
echo "Please also note that this script may be buggy."
echo