root/controlies/trunk/Plugins/Hosts.py @ 170
61 | manumora | ##############################################################################
|
|
# -*- coding: utf-8 -*-
|
|||
# Project: ControlIES
|
|||
# Module: Hosts.py
|
|||
# Purpose: Hosts class
|
|||
# Language: Python 2.5
|
|||
# Date: 7-Feb-2011.
|
|||
# Ver: 7-Feb-2011.
|
|||
# Author: Manuel Mora Gordillo
|
|||
# Francisco Mendez Palma
|
|||
# Copyright: 2011 - Manuel Mora Gordillo <manuito @no-spam@ gmail.com>
|
|||
# 2011 - Francisco Mendez Palma <fmendezpalma @no-spam@ gmail.com>
|
|||
#
|
|||
# ControlIES 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, either version 3 of the License, or
|
|||
# (at your option) any later version.
|
|||
# ControlIES 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
|
|||
91 | manumora | # along with ControlIES. If not, see <http://www.gnu.org/licenses/>.
|
|
61 | manumora | #
|
|
##############################################################################
|
|||
import ldap
|
|||
import logging
|
|||
89 | manumora | import time
|
|
61 | manumora | from math import ceil
|
|
73 | Chisco | from operator import itemgetter
|
|
61 | manumora | ||
class Hosts(object):
|
|||
def __init__(self):
|
|||
pass
|
|||
85 | Chisco | def __init__(self,ldap,name,ip,mac,group,type):
|
|
61 | manumora | self.ldap = ldap
|
|
self.name = name
|
|||
self.ip = ip
|
|||
self.mac = mac
|
|||
85 | Chisco | self.group = group
|
|
61 | manumora | self.type = type
|
|
def getName (self):
|
|||
return self.mac
|
|||
73 | Chisco | def validation(self,action):
|
|
if action == "add":
|
|||
if self.type == "none":
|
|||
return "type"
|
|||
61 | manumora | if self.name == "":
|
|
return "name"
|
|||
73 | Chisco | ||
if self.type <> 'thinclient':
|
|||
if self.ip == "":
|
|||
return "ip"
|
|||
61 | manumora | ||
if self.mac == "":
|
|||
return "mac"
|
|||
85 | Chisco | ||
if self.type =="thinclient":
|
|||
if self.group == "":
|
|||
return "group"
|
|||
73 | Chisco | ||
61 | manumora | if self.type == "":
|
|
return "type"
|
|||
73 | Chisco | if action == "add":
|
|
if self.existsHostname():
|
|||
return "hostAlreadyExists"
|
|||
# thinclients no requieren ip, la cogen dinamicamente en el aula
|
|||
if self.type <> "thinclient":
|
|||
if self.existsIP():
|
|||
return "ipAlreadyExists"
|
|||
94 | Chisco | #controlar overflow en nodos
|
|
if self.groupOverflow(300):
|
|||
return "groupOverflow"
|
|||
61 | manumora | ||
73 | Chisco | if self.existsMAC():
|
|
return "macAlreadyExists"
|
|||
113 | Chisco | elif action == "modify":
|
|
if self.groupOverflow(300):
|
|||
return "groupOverflow"
|
|||
73 | Chisco | ||
113 | Chisco | if self.existsMAC():
|
|
return "macAlreadyExists"
|
|||
73 | Chisco | return "OK"
|
|
61 | manumora | def process(self,action):
|
|
73 | Chisco | import pdb
|
|
61 | manumora | if action == "add":
|
|
73 | Chisco | val = self.validation(action)
|
|
61 | manumora | ||
if val != "OK":
|
|||
return val
|
|||
else:
|
|||
response = self.add()
|
|||
return response
|
|||
if action == "modify":
|
|||
113 | Chisco | val = self.validation(action)
|
|
61 | manumora | ||
if val != "OK":
|
|||
return val
|
|||
else:
|
|||
response = self.modify()
|
|||
return response
|
|||
if action == "delete":
|
|||
response = self.delete()
|
|||
return response
|
|||
if action == "list":
|
|||
response = self.list();
|
|||
return response
|
|||
def list(self,args):
|
|||
73 | Chisco | #from Plugins.LdapConnection import LdapConnection
|
|
#l = LdapConnection("172.23.36.5",'cn=admin,ou=People,dc=instituto,dc=extremadura,dc=es',"Sta1987teleco")
|
|||
#l.connect()
|
|||
# grid parameters
|
|||
61 | manumora | limit = int(args['rows'][0])
|
|
page = int(args['page'][0])
|
|||
73 | Chisco | start = limit * page - limit
|
|
finish = start + limit;
|
|||
# sort by field
|
|||
sortBy = args['sidx'][0]
|
|||
#if sortBy == "uid":
|
|||
#sortBy = "id"
|
|||
# reverse Sort
|
|||
reverseSort = False
|
|||
if args['sord'][0] == "asc":
|
|||
reverseSort = True
|
|||
#Distinguimos entre hosts ltsp, workstations y portatiles
|
|||
61 | manumora | type = args['type'][0]
|
|
73 | Chisco | ||
if type == "ltsp":
|
|||
# Obtengo todos los elementos del nodo hosts
|
|||
#search = l.search("ou=hosts","cn=*",["cn","ipHostNumber","macAddress"])
|
|||
#filter = self.buildFilter(args)
|
|||
search = self.ldap.search("ou=hosts","cn=*",["cn","ipHostNumber","macAddress"])
|
|||
61 | manumora | ||
73 | Chisco | # triplets que contiene los nombres de los ltsp-servers
|
|
89 | manumora | hostnames = self.getLTSPServers()
|
|
73 | Chisco | # Ahora tengo que quedarme con los elementos de search que estan en hostnames: los que son ltsp
|
|
resultado=list()
|
|||
for element in search:
|
|||
if element[0][1]["cn"][0] in hostnames:
|
|||
resultado.append(element)
|
|||
search = resultado
|
|||
rows = []
|
|||
94 | Chisco | for i in search:
|
|
73 | Chisco | row = {
|
|
"id":i[0][1]["cn"][0],
|
|||
"cell":[i[0][1]["cn"][0], i[0][1]["ipHostNumber"][0],i[0][1]["macAddress"]],
|
|||
"cn":i[0][1]["cn"][0],
|
|||
"ipHostNumber":i[0][1]["ipHostNumber"][0],
|
|||
"macAddress":i[0][1]["macAddress"][0]
|
|||
106 | Chisco | }
|
|
73 | Chisco | #row = { "cn":i[0][0], "cell":[i[0][1]["cn"][0], i[0][1]["ipHostNumber"][0],i[0][1]["macAddress"]]}
|
|
rows.append(row)
|
|||
94 | Chisco | if len(rows) > 0:
|
|
totalPages = ceil( len(rows) / int(limit) )
|
|||
else:
|
|||
totalPages = 0
|
|||
if page > totalPages:
|
|||
page = totalPages
|
|||
73 | Chisco | result = sorted(rows, key=itemgetter(sortBy), reverse=reverseSort)
|
|
94 | Chisco | return { "page":page, "total":totalPages, "records":len(rows), "rows":result[start:finish] }
|
|
73 | Chisco | ||
elif type == "thinclient":
|
|||
106 | Chisco | search = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","cn=*",["cn","dhcpHWAddress"])
|
|
filter="(|(dhcpOption=*subnet*)(dhcpOption=*log*))"
|
|||
import pdb
|
|||
73 | Chisco | ||
rows = []
|
|||
106 | Chisco | ||
73 | Chisco | # esto hay que cambiarlo: tenemos 4 groups en thinclientes
|
|
94 | Chisco | for i in search[6:len(search)]:
|
|
106 | Chisco | nodeinfo=i[0][0].replace ("cn=","").split(",")
|
|
73 | Chisco | row = {
|
|
"id":i[0][1]["cn"][0],
|
|||
106 | Chisco | "cell":[i[0][1]["cn"][0], i[0][1]["dhcpHWAddress"][0].replace("ethernet ",""), nodeinfo[1]],
|
|
73 | Chisco | "cn":i[0][1]["cn"][0],
|
|
106 | Chisco | "dhcpHWAddress":i[0][1]["dhcpHWAddress"][0],
|
|
"groupName":i[0][1]["dhcpHWAddress"][0]
|
|||
73 | Chisco | }
|
|
rows.append(row)
|
|||
94 | Chisco | if len(rows) > 0:
|
|
totalPages = ceil( len(rows) / int(limit) )
|
|||
else:
|
|||
totalPages = 0
|
|||
if page > totalPages:
|
|||
page = totalPages
|
|||
73 | Chisco | result = sorted(rows, key=itemgetter(sortBy), reverse=reverseSort)
|
|
94 | Chisco | return { "page":page, "total":totalPages, "records":len(rows), "rows":result[start:finish] }
|
|
61 | manumora | ||
73 | Chisco | elif type == "workstation":
|
|
# Obtengo todos los elementos del nodo hosts
|
|||
#search = l.search("ou=hosts","cn=*",["cn","ipHostNumber","macAddress"])
|
|||
search = self.ldap.search("ou=hosts","cn=*",["cn","ipHostNumber","macAddress"])
|
|||
# triplets que contiene los nombres de las workstations
|
|||
#triplets = l.search("ou=Netgroup","cn=workstation-hosts",["nisNetgroupTriple"])
|
|||
triplets = self.ldap.search("ou=Netgroup","cn=workstation-hosts",["nisNetgroupTriple"])
|
|||
triplets = triplets [0][0][1]["nisNetgroupTriple"]
|
|||
hostnames=list()
|
|||
# obtengo lista de nombres de los hosts workstation
|
|||
for node in triplets:
|
|||
name = node.replace(",-,-)","").replace("(","")
|
|||
hostnames.append(name)
|
|||
# Ahora tengo que quedarme con los elementos de search que estan en hostnames
|
|||
resultado=list()
|
|||
for element in search:
|
|||
if element[0][1]["cn"][0] in hostnames:
|
|||
resultado.append(element)
|
|||
search = resultado
|
|||
61 | manumora | ||
73 | Chisco | rows = []
|
|
94 | Chisco | for i in search:
|
|
73 | Chisco | row = {
|
|
"id":i[0][1]["cn"][0],
|
|||
"cell":[i[0][1]["cn"][0], i[0][1]["ipHostNumber"][0],i[0][1]["macAddress"]],
|
|||
"cn":i[0][1]["cn"][0],
|
|||
"ipHostNumber":i[0][1]["ipHostNumber"][0],
|
|||
"macAddress":i[0][1]["macAddress"][0]
|
|||
}
|
|||
#row = { "cn":i[0][0], "cell":[i[0][1]["cn"][0], i[0][1]["ipHostNumber"][0],i[0][1]["macAddress"]]}
|
|||
rows.append(row)
|
|||
61 | manumora | ||
94 | Chisco | if len(rows) > 0:
|
|
totalPages = ceil( len(rows) / int(limit) )
|
|||
else:
|
|||
totalPages = 0
|
|||
if page > totalPages:
|
|||
page = totalPages
|
|||
73 | Chisco | result = sorted(rows, key=itemgetter(sortBy), reverse=reverseSort)
|
|
94 | Chisco | return { "page":page, "total":totalPages, "records":len(rows), "rows":result[start:finish] }
|
|
61 | manumora | ||
def add(self):
|
|||
94 | Chisco | if self.type=="thinclient":
|
|
attr = [
|
|||
('objectclass', ['top','dhcpHost']),
|
|||
('cn', [self.name] ),
|
|||
('dhcpStatements', ['filename "/var/lib/tftpboot/ltsp/i386/pxelinux.0"'] ),
|
|||
('dhcpHWAddress', ['ethernet ' + self.mac] )
|
|||
]
|
|||
106 | Chisco | self.ldap.add("cn="+self.name +",cn="+self.group+",cn=THINCLIENTS,cn=DHCP Config", attr)
|
|
75 | Chisco | ||
61 | manumora | return "OK"
|
|
75 | Chisco | ||
61 | manumora | def modify(self):
|
|
113 | Chisco | attr = [
|
|
(ldap.MOD_REPLACE, 'cn', [self.name] ),
|
|||
(ldap.MOD_REPLACE, 'dhcpHWAddress', ["ethernet "+ self.mac])
|
|||
61 | manumora | ]
|
|
113 | Chisco | ||
self.ldap.modify("cn="+self.name+",cn="+self.group +",cn=THINCLIENTS,cn=DHCP Config", attr)
|
|||
61 | manumora | ||
113 | Chisco | return "OK"
|
|
106 | Chisco | def delete(self):
|
|
if self.type=="thinclient":
|
|||
self.ldap.delete('cn='+ self.name +',cn=' +self.group +',cn=THINCLIENTS,cn=DHCP Config')
|
|||
61 | manumora | ||
106 | Chisco | return "OK"
|
|
61 | manumora | def wakeup(self):
|
|
from Plugins import NetworkUtils
|
|||
NetworkUtils.startup(self.mac)
|
|||
73 | Chisco | ||
94 | Chisco | def groupOverflow(self,overflow):
|
|
import pdb
|
|||
if self.type == 'thinclient':
|
|||
search = self.ldap.search("cn=" + self.group +",cn=THINCLIENTS,cn=DHCP Config","cn=*",["cn"])
|
|||
if len(search)-2 >= overflow:
|
|||
return True
|
|||
return False
|
|||
73 | Chisco | def existsHostname(self):
|
|
75 | Chisco | ||
73 | Chisco | if self.type == 'thinclient':
|
|
75 | Chisco | result = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","cn="+self.name,["cn"])
|
|
73 | Chisco | else:
|
|
result = self.ldap.search("ou=hosts","cn="+self.name,["cn"])
|
|||
if len(result) > 0:
|
|||
return True
|
|||
return False
|
|||
def existsMAC(self):
|
|||
# Compruebo con las macs de la rama hosts
|
|||
if self.type == 'thinclient':
|
|||
75 | Chisco | result = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","dhcpHWAddress=*",["dhcpHWAddress"])
|
|
for i in range (0, len(result) - 1):
|
|||
if result [i][0][1]['dhcpHWAddress'][0].replace ("ethernet ", "") == self.mac:
|
|||
return True
|
|||
73 | Chisco | else:
|
|
result = self.ldap.search("ou=hosts","macAddress="+self.mac,["macAddress"])
|
|||
75 | Chisco | if len(result) > 0:
|
|
return True
|
|||
73 | Chisco | ||
return False
|
|||
def existsIP (self):
|
|||
# Cojo las ips de la rama hosts -> arpa -> in-addr
|
|||
result = self.ldap.search ("dc=23,dc=172,dc=in-addr,dc=arpa,ou=hosts", "dc=*",["associatedDomain"])
|
|||
myIP = self.ip.split (".")
|
|||
for i in range (0, len (result) -1):
|
|||
reverseIP = result [i][0][1]['associatedDomain'][0].replace (".in-addr.arpa","").split(".")
|
|||
reverseIP.reverse()
|
|||
if myIP == reverseIP:
|
|||
return True
|
|||
return False
|
|||
75 | Chisco | def getThinclientGroups (self):
|
|
73 | Chisco | ||
75 | Chisco | groups = []
|
|
search = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","cn=group*",["cn"])
|
|||
for g in search:
|
|||
groups.append (g[0][1]["cn"][0])
|
|||
return { "groups":groups }
|
|||
89 | manumora | ||
119 | Chisco | def getInternalGroups (self):
|
|
groups = []
|
|||
search = self.ldap.search("cn=INTERNAL,cn=DHCP Config","cn=group*",["cn"])
|
|||
for g in search:
|
|||
groups.append (g[0][1]["cn"][0])
|
|||
return { "groups":groups }
|
|||
113 | Chisco | def getHostData(self):
|
|
#self.getUserGroups()
|
|||
if self.type == "thinclient":
|
|||
result = self.ldap.search("cn=THINCLIENTS,cn=DHCP Config","cn="+self.name,["cn","dhcpHWAddress"])
|
|||
89 | manumora | ||
113 | Chisco | dataHost = {
|
|
"cn":result[0][0][1]["cn"][0],
|
|||
"mac":result[0][0][1]["dhcpHWAddress"][0].replace("ethernet ",""),
|
|||
"group":self.group
|
|||
}
|
|||
return dataHost
|
|||
89 | manumora | def getLTSPServers (self):
|
|
triplets = self.ldap.search("ou=Netgroup","cn=ltsp-server-hosts",["nisNetgroupTriple"])
|
|||
triplets = triplets [0][0][1]["nisNetgroupTriple"]
|
|||
hostnames=list()
|
|||
for node in triplets:
|
|||
name = node.replace(",-,-)","").replace("(","")
|
|||
hostnames.append(name)
|
|||
hostnames.sort()
|
|||
return hostnames
|
|||
def getLTSPStatus (self):
|
|||
from Utils.avahiClient import avahiClient
|
|||
90 | manumora | import threading
|
|
89 | manumora | ||
90 | manumora | a = avahiClient()
|
|
a.start()
|
|||
time.sleep(1000)
|
|||
a.cancel()
|
|||
names = a.getList()
|
|||
print names
|
|||
"""a = avahiClient()
|
|||
89 | manumora | time.sleep(1000)
|
|
names = a.getList()
|
|||
print names
|
|||
90 | manumora | a.kill()"""
|
|
89 | manumora | return names
|
|
61 | manumora | # def wakeup(self):
|
|
# from twisted.internet.task import LoopingCall
|
|||
# from twisted.internet import defer
|
|||
# from Plugins import NetworkUtils
|
|||
# NetworkUtils.startup(self.mac)
|
|||
# Encender el equipo
|
|||
#def wakeup(self):
|
|||
# macs=[]
|
|||
# for i in self.targets:
|
|||
# mac=Configs.MonitorConfigs.GetMAC(i)
|
|||
# if mac !='':
|
|||
# macs.append(mac)
|
|||
# Actions.sendWOLBurst(macs, 2)
|
|||
#Apagar el equipo
|
|||
# def sleep(self):
|
|||
# self.usersCommand(Desktop.sleep)
|
|||
#def sendWOLBurst(macs,throttle):
|
|||
# from twisted.internet.task import LoopingCall
|
|||
# from twisted.internet import defer
|
|||
# if not macs:
|
|||
# return defer.succeed(None)
|
|||
# d = defer.Deferred()
|
|||
# work = list(macs)
|
|||
# def sendNext():
|
|||
# if not work:
|
|||
# loop.stop()
|
|||
# d.callback(None)
|
|||
# return defer.succeed(None)
|
|||
# next = work.pop(0)
|
|||
#
|
|||
# #subprocess.Popen(['wakeonlan',next ])
|
|||
# #subprocess.Popen(['wakeonlan','-i','192.168.0.255',next ])
|
|||
# NetworkUtils.startup(next)
|
|||
#
|
|||
# return None
|
|||
# loop = LoopingCall(sendNext)
|
|||
# loop.start(throttle)
|
|||
# return d
|