109 lines
3 KiB
Python
Executable file
109 lines
3 KiB
Python
Executable file
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# slon-get-volume-mapping.py
|
|
# --------------------------
|
|
# Copyright : (c) 2008, Jeremie Dimino <dimino@crans.org>
|
|
# Licence : BSD3
|
|
|
|
u'''Outil pour récupérer le mapping lun/volume depuis la baie de
|
|
stockage'''
|
|
|
|
import telnetlib, re, sys
|
|
|
|
sys.path.append("/usr/scripts/gestion")
|
|
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.append((int(m.group(2)), m.group(1)))
|
|
map.sort()
|
|
|
|
print u"Enregistrement des informations..."
|
|
|
|
f = open(ISCSI_MAP_FILE, "w")
|
|
f.write((u"""\
|
|
# -*- coding: utf-8 -*-
|
|
# Fichier de mapping lun -> nom de volume
|
|
#
|
|
# Ce fichier est généré par %s
|
|
|
|
map = {
|
|
""" % sys.argv[0]).encode("UTF-8"))
|
|
|
|
for lun, name in map:
|
|
f.write(' %d : "%s",\n' % (lun, name))
|
|
|
|
f.write("}\n")
|
|
|
|
f.close()
|
|
|
|
print u"Terminé, mapping enregistré dans %s" % ISCSI_MAP_FILE
|
|
|
|
print u"Le mapping actuel est:"
|
|
print affich_tools.tableau(titre = ["lun", "nom"], data = map, alignement = ["g", "c"])
|