root/trunk/umitInventory/InventoryLoad.py @ 4097

Revision 4097, 4.9 kB (checked in by luis, 4 years ago)

Updated licence copyrights using -> find . -path '*/.svn' -prune -o -type f -print0 | xargs -0 sed -i -e 's/2007 Insecure.Com LLC./2007 Adriano Monteiro Marques/'

Line 
1# Copyright (C) 2007 Adriano Monteiro Marques
2#
3# Authors: Guilherme Polo <ggpolo@gmail.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18# USA
19
20import os
21from ConfigParser import ConfigParser
22
23from umitCore.Paths import Path
24from umitDB.Connection import ConnectDB
25from umitDB.Retrieve import InventoryRetrieve
26
27UMITDB = Path.umitdb_ng
28
29class InventoryLoad(ConnectDB, InventoryRetrieve):
30    """
31    Load Inventories data to be used in Network Inventory.
32    """
33   
34    def __init__(self):
35        ConnectDB.__init__(self, UMITDB)
36        InventoryRetrieve.__init__(self, self.conn, self.cursor)
37
38        self.invdata = None
39        self.database_stat = None
40
41       
42    def load_from_db(self):
43        """
44        Load all inventories from database and return a dict in this format:
45       
46        inv_data = {'inventory A': set([(ipv4addr A, ipv6addr A, macaddr A,
47                                         (hostnameA, hostnameB, ..),
48                                         osmatch A),
49                                        (ipv4addr B, ipv6addr B, macaddr B,
50                                         (hostnameC, ..), osmatch B), ...
51                                        ]),
52                    'inventory B': ...
53                   }
54       
55        Each item inside set represent a host inside the 'inventory name'.
56        """
57
58        inv_data = { }
59
60        # Grab Inventories names
61        # from schemas file
62        schemas = ConfigParser()
63        schemas.read(Path.sched_schemas)
64
65        for section in schemas.sections():
66            if schemas.get(section, 'addtoinv') in ('1', '2'):
67                inv_data[section] = set()
68        # from database
69        invs = self.get_inventories_names()
70        for i in invs:
71            if not i[0] in inv_data:
72                # inventory created from umit interface
73                inv_data[i[0]] = set()
74
75        # check if database haven't changed since last time
76        if self.database_stat == os.stat(UMITDB).st_mtime:
77            # data has been loaded already, maybe a new Inventory was
78            # added in schemas file only, let me check here:
79            n_invs = set(inv_data.keys()).difference(set(self.invdata.keys()))
80            for new_i in n_invs:
81                self.invdata[new_i] = set()
82
83            # return cached data with new inventories if any
84            return self.invdata
85
86        self.database_stat = os.stat(UMITDB).st_mtime
87
88        # XXX ToDo: it would be good to do some kind of caching here or in
89        # database, so we can stop doing so many queries.
90
91        # Retrieve hosts for each Inventory
92        for inv in inv_data.keys():
93            invid = self.get_inventory_id_for_name(inv)
94
95            if not invid:
96                # this Inventory has been created but it is not on database
97                # yet because no scan has been finished yet.
98                continue
99
100            scans_ids = self.get_scans_id_for_inventory(invid)
101
102            if not scans_ids:
103                # this Inventory has no associated scans with it, one possible
104                # reason: all scans for the Inventory were deleted
105                continue
106
107            for scan in scans_ids:
108                scan_hosts = set() # will store hosts in current scan
109
110                # retrieve hosts id for each scan
111                hosts = self.get_hosts_id_for_scan_from_db(scan[0])
112                hosts = [h[0] for h in hosts]
113
114                if len(hosts) <= len(inv_data[inv]):
115                    # no new hosts found
116                    continue
117
118                #for host in self.get_hosts_id_for_scan_from_db(scan[0]):
119                for host in hosts:
120
121                    os_host = self.get_osshort_for_host_from_db(host)
122                    ipv4addr = self.get_ipv4_for_host_from_db(host)
123                    ipv6addr = self.get_ipv6_for_host_from_db(host)
124                    macaddr = self.get_mac_for_host_from_db(host)
125                    hostnames = self.get_hostnames_for_host_from_db(host)
126
127                    scan_hosts.add((ipv4addr, ipv6addr, macaddr, hostnames,
128                         os_host))
129
130                if scan_hosts != inv_data[inv] and \
131                   len(scan_hosts) > len(inv_data[inv]): # new hosts found
132                    inv_data[inv] = scan_hosts
133
134        self.invdata = inv_data
135        return inv_data
Note: See TracBrowser for help on using the browser.