From edc0315878db3c11ed9b87fc6dd9315c0d81669d Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Tue, 16 Dec 2008 05:10:08 +0100 Subject: [PATCH] =?UTF-8?q?[slon-get-volume-mapping]=20script=20pour=20r?= =?UTF-8?q?=C3=A9cup=C3=A9rer=20le=20mapping=20des=20volumes=20iscsi=20en?= =?UTF-8?q?=20telnet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit darcs-hash:20081216041008-c41ad-cd632cdd9e2df777b356f7250dc5d87e8b1884e5.gz --- gestion/slon-get-volume-mapping.py | 93 ++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100755 gestion/slon-get-volume-mapping.py diff --git a/gestion/slon-get-volume-mapping.py b/gestion/slon-get-volume-mapping.py new file mode 100755 index 00000000..b79d9224 --- /dev/null +++ b/gestion/slon-get-volume-mapping.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# slon-get-volume-mapping.py +# -------------------------- +# Copyright : (c) 2008, Jeremie Dimino +# Licence : BSD3 + +u'''Outil pour récupérer le mapping lun/volume depuis la baie de +stockage''' + +import telnetlib, re +from config import ISCSI_MAP_FILE +import affich_tools + +# Ce script utilise l'interface telnet de la baie. + +# La réponse à la commande "show volume-map" est une séquence +# d'éléments de la forme: +# +# nom du volume +# | +# v +# > Volume [SN 00c0ffd5e51800004ca1454901000000, Name (o2_slash)] mapping view: +# > CH ID LUN Access Host-Port-Identifier Nickname +# > ------------------------------------------------------------------------------- +# > 0,1 0 4 rw all other hosts +# ^ +# | +# lun + +# On utilise cette expression un peu barbare pour récupérer les deux +# informations qui nous intéressses: +volume_regexp = re.compile("Volume \[SN [0-9a-f]+, Name \(([^)]*)\)\][^\n]*\n[^\n]*\n[^\n]*\n[0-9,-]+ *[0-9]+ *([0-9]+)[^\n]*\n") + +username = "" +password = "" + +# Récupération des identifiants. Cela écrase les deux variables +# username et password: +execfile("/etc/crans/secrets/slon.py") + +print u"Connection à la baie de stockage..." + +tn = telnetlib.Telnet("slon.adm.crans.org") + +# Identification +tn.read_until("Login: ") +tn.write(username + "\r\n") +tn.read_until("Password: ") +tn.write(password + "\r\n") + +# Ça c'est pour attendre la l'invite de commande: +tn.read_until("#") + +print u"Récupération des informations..." + +tn.write("show volume-maps" + "\r\n") +resp = "" +# Lecture de la réponse, c'est terminé quand on atteint un nouveau +# prompt: +while resp.find("#") < 0: + resp = resp + tn.read_some() + # Parfois le serveur attend que l'on appuie sur une touche pour + # continuer à envoyer les données: + if resp.endswith("Press any key to continue (Q to quit)"): + tn.write(" ") + +print u"Déconnection..." + +tn.write("exit\r\n") +tn.read_all() +tn.close() + +# On retire les messages parasites +junk = re.compile("Press any key to continue \(Q to quit\)\x0d *\x0d") +resp = junk.sub("", resp) + +# Parsing de la réponse +map={} +for m in volume_regexp.finditer(resp): + map[int(m.group(2))] = m.group(1) + +print u"Enregistrement des informations..." + +file(ISCSI_MAP_FILE, "w").write("map = %s\n" % str(map)) + +print u"Terminé, mapping enregistré dans %s" % ISCSI_MAP_FILE + +print u"Le mapping actuel est:" +l = [[lun, name] for lun, name in map.iteritems()] +l.sort() +print affich_tools.tableau(titre = ["lun", "nom"], data = l, alignement = ["g", "c"])