ProgrammersOfVilnius / pov-check-health

Debian package that runs basic system health monitoring checks hourly from cron
https://launchpad.net/~pov/+archive/ppa
2 stars 0 forks source link

Wishlist: checklocale #16

Closed mgedmin closed 5 years ago

mgedmin commented 8 years ago

I had a bug where a web server process had the incorrect locale and would sometimes crash when trying to create files with non-ASCII filenames.

checklocale() {
    info_check checklocale "$@"
    # usage: checklocale LOCALE pgrep-args
    # check that a process found by pgrep uses the specified locale
    # example: checklocale '*.UTF-8' someprogram
    shouldbe=$1
    shift
    pid=$(pgrep "$@")
    test $(echo "$pid"|wc -l) -gt 1 && {
        # there's a race condition: when zope forks a child to run aspell, the child also appears to be zope for a brief moment
        # hopefully 100ms is enough for it to do the exec, and hopefully we won't encounter a second race so quickly
        sleep 0.1
        pid=$(pgrep "$@")
    }
    test -z "$pid" && {
        warn "no process found by pgrep $@"
        return 1
    }
    test $(echo "$pid"|wc -l) -gt 1 && {
        warn "more than one process found by pgrep $@:" $pid
        ps $pid
        return 1
    }
    locale=$(tr '\0' '\n' < /proc/$pid/environ|grep -e ^LC_ALL=)
    test -z "$locale" && locale=$(tr '\0' '\n' < /proc/$pid/environ|grep -e ^LC_CTYPE=)
    test -z "$locale" && locale=$(tr '\0' '\n' < /proc/$pid/environ|grep -e ^LANG=)
    case "${locale#*=}" in
      $shouldbe) ;;
      *)
        warn "$@ ($pid) has locale $locale instead of $shouldbe"
        return 1
        ;;
    esac
}