sysadmin : ajout d'un affichage des objectifs.
[progfou.git] / sysadmin / sysadmin-check-version-arch
1 #!/bin/sh
2 # sysadmin-check-version-arch - vérifie la version et l'architecture système
3 # Copyright ©2011  Agence universitaire de la Francophonie
4 #                  http://www.auf.org/
5 # Licence : GNU General Public License, version 3
6 # Auteur : Progfou <jean-christophe.andre@auf.org>
7 # Création : 2011-04-26
8 # Mise à jour : 2011-08-23
9
10 EXPECTED_DEBIAN_VERSION="6.0.2"
11 ACCEPTED_DEBIAN_VERSION="5.0.8"
12 if grep -q '^flags.* lm ' /proc/cpuinfo ; then
13   EXPECTED_DEBIAN_ARCH="x86_64"
14   ACCEPTED_DEBIAN_ARCH="i686"
15 else
16   EXPECTED_DEBIAN_ARCH="i686"
17   ACCEPTED_DEBIAN_ARCH="i686"
18 fi
19
20 EXPECTED_UBUNTU_VERSION="10.04"
21 ACCEPTED_UBUNTU_VERSION="10.04"
22 if dpkg -l ubuntu-desktop >/dev/null 2>&1 ; then
23   EXPECTED_UBUNTU_ARCH="i686"
24   ACCEPTED_UBUNTU_ARCH="i686"
25 else # serveur sous Ubuntu
26   EXPECTED_UBUNTU_ARCH="${EXPECTED_DEBIAN_ARCH}"
27   ACCEPTED_UBUNTU_ARCH="${ACCEPTED_DEBIAN_ARCH}"
28 fi
29
30 color_name_to_code() {
31   case "$1" in
32     black)   printf "0" ;;
33     red)     printf "1" ;;
34     green)   printf "2" ;;
35     yellow)  printf "3" ;;
36     blue)    printf "4" ;;
37     magenta) printf "5" ;;
38     cyan)    printf "6" ;;
39     white)   printf "7" ;;
40     *)       printf ""  ;;
41   esac
42 }
43
44 colorize() {
45   # syntax: colorize [bright] [bg color_name] color_name text
46   ansi_seq=""
47   if [ "$1" = "bright" ] ; then
48     shift ; ansi_seq="${ansi_seq}1;"
49   fi
50   if [ "$1" = "bg" ] ; then
51     shift ; bg="`color_name_to_code "$1"`" ; shift
52     if [ "${bg}" != "" ] ; then
53       ansi_seq="${ansi_seq}4${bg};"
54     fi
55   fi
56   fg="`color_name_to_code "$1"`" ; shift
57   if [ "${fg}" != "" ] ; then
58       ansi_seq="${ansi_seq}3${fg}"
59   fi
60   text="$@"
61   if [ "${ansi_seq}" != "" ] ; then
62     text="\033[${ansi_seq}m${text}\033[m"
63   fi
64   printf "${text}"
65 }
66
67 display_host() {
68   name="$1" ; dist="$2" ; version="$3" ; arch="$4" ; note="$5"
69   goals=""
70   if [ "${dist}" = "Ubuntu" ] ; then
71     expected_version="${EXPECTED_UBUNTU_VERSION}"
72     accepted_version="${ACCEPTED_UBUNTU_VERSION}"
73     expected_arch="${EXPECTED_UBUNTU_ARCH}"
74     accepted_arch="${ACCEPTED_UBUNTU_ARCH}"
75   elif [ "${dist}" = "Debian" ] ; then
76     expected_version="${EXPECTED_DEBIAN_VERSION}"
77     accepted_version="${ACCEPTED_DEBIAN_VERSION}"
78     expected_arch="${EXPECTED_DEBIAN_ARCH}"
79     accepted_arch="${ACCEPTED_DEBIAN_ARCH}"
80   else
81     dist="`colorize bright red "${dist}"`"
82     goals="${goals} Debian"
83   fi
84   if [ "${version}" = "${expected_version}" ] ; then
85     version="`colorize bright green "${version}"`"
86   else
87     goals="${goals} ${expected_version}"
88     if [ "${version}" != "${accepted_version}" ] ; then
89       version="`colorize bright red "${version}"`"
90     fi
91   fi
92   if [ "${arch}" = "${expected_arch}" ] ; then
93     arch="`colorize bright green "${arch}"`"
94   else
95     goals="${goals} ${expected_arch}"
96     if [ "${arch}" != "${accepted_arch}" ] ; then
97       arch="`colorize bright red "${arch}"`"
98     fi
99   fi
100   if [ "${note}" = "voip" ] ; then
101     if [ "${version}" = "${accepted_version}" ] ; then
102       version="`colorize bright green "${version}"`"
103     fi
104     goals=""
105     note='ne pas migrer vers Squeeze !'
106   fi
107   if expr match "${note}" '.*!' >/dev/null ; then
108     note="`colorize bright red "${note}"`"
109   fi
110   if [ "${goals}" != "" ] ; then
111     goals="`colorize bright blue "${goals}"`"
112     note="${note} (but :${goals})"
113   fi
114   printf "%-25s: %s %s (%s) %s\n" "${name}" "${dist}" "${version}" "${arch}" "${note}"
115 }
116
117 display_host "host" "`lsb_release -si`" "`lsb_release -sr`" "`uname -m`"
118 if test -x /usr/sbin/vzctl
119 then
120   IDS="`vzlist -H -o ctid`"
121   if [ -n "${IDS}" ] ; then
122     for id in ${IDS}
123     do
124       note=""
125       name="`sed -n '/^NAME=/s/^.*="\?\([^"]*\)"\?.*$/\1/p' /etc/vz/conf/${id}.conf`"
126       if [ -z "${name}" ] ; then
127         name="`sed -n '/^HOSTNAME=/s/^.*="\?\([^"]*\)"\?.*$/\1/p' /etc/vz/conf/${id}.conf`"
128       fi
129       dist="`vzctl exec ${id} lsb_release -si 2>/dev/null`"
130       if [ -n "${dist}" ] ; then
131         version="`vzctl exec ${id} lsb_release -sr 2>/dev/null`"
132       else
133         dist="------"
134         version="-----"
135         note='paquet lsb-release manquant !'
136       fi
137       arch="`vzctl exec ${id} uname -m`"
138       if [ -x "/var/lib/vz/private/${id}/usr/sbin/asterisk" ] ; then
139         note="voip"
140       fi
141       display_host "CT ${id} (${name})" "${dist}" "${version}" "${arch}" "${note}"
142     done
143   fi
144 fi