surveillance.py 3.63 KB
Newer Older
chove's avatar
chove committed
1
#! /usr/bin/env python
2
# -*- coding: utf-8 -*-
chove's avatar
chove committed
3
4
5
6
7

"""
Classe de synchronisation entre la base ldap et
la base postgresql pour la liste des exemptions
et la liste des machines.
8
Utilisé par generate.py
chove's avatar
chove committed
9
10
11
12
13
14
"""

# importation des fonctions et classes

import sys, os, commands
sys.path.append('/usr/scripts/gestion')
glondu's avatar
glondu committed
15
from ldap_crans import crans_ldap, Club, Adherent, AssociationCrans
chove's avatar
chove committed
16
from gen_confs import gen_config
17
import psycopg2
chove's avatar
chove committed
18

glondu's avatar
glondu committed
19
20
db = crans_ldap()

21
# Génération des la tables d'exemptions
chove's avatar
chove committed
22
23
24
25
26
27
28
29
30
31
32
33
#######################################

class exemptions(gen_config) :
    
    base=[('138.231.136.0/28','0.0.0.0/0'),('138.231.136.0/21','138.231.0.0/16'),('138.231.148.0/22','138.231.0.0/16')]
    restart_cmd = ""
    
    def __str__ (self) :
        return "surveillance_exemptions"
    
    def _gen (self) :
        
glondu's avatar
glondu committed
34
        machines = db.search('exempt=*')['machine']
35
        pgsql = psycopg2.connect(database='filtrage', user='crans')
chove's avatar
chove committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        curseur = pgsql.cursor()
        
        requete="DELETE FROM exemptes"
        curseur.execute(requete)
        
        for source,destination in self.base :
            requete="INSERT INTO exemptes (ip_crans,ip_dest) VALUES ('%s','%s')" % (source,destination)
            curseur.execute(requete)
            
        for machine in machines :
            for destination in machine.exempt() :
                requete="INSERT INTO exemptes (ip_crans,ip_dest) VALUES ('%s','%s')" % (machine.ip(),destination)
                curseur.execute(requete)
        
        pgsql.commit()

52
# Génération des la liste des machines
chove's avatar
chove committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
######################################

class machines(gen_config) :
    
    restart_cmd = ""
    
    def __str__ (self) :
        return "surveillance_liste_machines"
            
    def _gen (self) :
                
        # recherche de toutes les machines dans ldap
        import sys
        sys.path.append('/usr/scripts/gestion')
glondu's avatar
glondu committed
67
        machines = db.search('ip=*')['machine']
chove's avatar
chove committed
68
69
        
        # liste des machines (on prend que les paimement ok)
70
        # machines = self.machines
glondu's avatar
glondu committed
71
        # machines = db.search('ip=*')['machine']
chove's avatar
chove committed
72
        
73
        # connexion à la base postgresql
74
75
        import psycopg2
        pgsql = psycopg2.connect(database='filtrage', user='crans')
chove's avatar
chove committed
76
77
        curseur = pgsql.cursor()
        curseur.execute("DELETE FROM machines;")
78
79
80
81
82
83

        ipv6_vu={}
        def ipv6_already_set(ipv6):
            ret = ipv6_vu.get(ipv6, False)
            ipv6_vu[ipv6] = True
            return ret
chove's avatar
chove committed
84
        
85
        # ajout des entrée
chove's avatar
chove committed
86
        for m in machines:
glondu's avatar
glondu committed
87
            if m.proprietaire().__class__ == Club:
chove's avatar
chove committed
88
                curseur.execute("INSERT INTO machines (ip, type, id) VALUES (inet'%s','club',%s);"%(m.ip(),m.proprietaire().id()))
89
90
                if not ipv6_already_set(m.ipv6()):
                    curseur.execute("INSERT INTO machines (ip, type, id) VALUES (inet'%s','club',%s);"%(m.ipv6(),m.proprietaire().id()))
glondu's avatar
glondu committed
91
            elif m.proprietaire().__class__ == Adherent:
chove's avatar
chove committed
92
                curseur.execute("INSERT INTO machines (ip, type, id) VALUES (inet'%s','adherent',%s);"%(m.ip(),m.proprietaire().id()))
93
94
                if not ipv6_already_set(m.ipv6()):
                    curseur.execute("INSERT INTO machines (ip, type, id) VALUES (inet'%s','adherent',%s);"%(m.ipv6(),m.proprietaire().id()))
glondu's avatar
glondu committed
95
            elif m.proprietaire().__class__ == AssociationCrans:
chove's avatar
chove committed
96
                curseur.execute("INSERT INTO machines (ip, type, id) VALUES (inet'%s','crans',%s);"%(m.ip(),m.id()))
97
98
                if not ipv6_already_set(m.ipv6()):
                    curseur.execute("INSERT INTO machines (ip, type, id) VALUES (inet'%s','crans',%s);"%(m.ipv6(),m.id()))
chove's avatar
chove committed
99
100
101
        # on commit
        pgsql.commit()