Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / plugins / vigilo / inc / abstractmonitoreditem.class.php @ c8bad135

History | View | Annotate | Download (5.41 KB)

1
<?php
2

    
3
abstract class PluginVigiloAbstractMonitoredItem extends VigiloXml
4
{
5
    protected $item;
6
    protected $addresses;
7
    protected $ventilation;
8
    protected $children;
9
    protected $agent;
10

    
11
    public function __construct(CommonDBTM $item)
12
    {
13
        $this->agent        = null;
14
        $this->item         = $item;
15
        $this->ventilation  = "Servers";
16
        $this->addresses    = array();
17
        $this->children     = array();
18

    
19
        if (class_exists('PluginFusioninventoryAgent')) {
20
            $agent = new PluginFusioninventoryAgent();
21
            if ($agent->getAgentWithComputerid($item->getID()) !== false) {
22
                $this->agent = $agent;
23
            }
24
        }
25

    
26
        $this->selectTemplates();
27
        $this->selectGroups();
28
        $this->monitorNetworkInterfaces();
29
    }
30

    
31
    public function getName()
32
    {
33
        return $this->item->getName();
34
    }
35

    
36
    protected function selectTemplates()
37
    {
38
        $template = $this->item->fields['vigilo_template'];
39
        if (null !== $template) {
40
            $this->children[] = new VigiloTemplate($template);
41
        }
42
    }
43

    
44
    protected function selectGroups()
45
    {
46
        $location = new Location();
47
        $location->getFromDB($this->item->fields["locations_id"]);
48

    
49
        $entity = new Entity();
50
        $entity->getFromDB($this->item->fields["entities_id"]);
51

    
52
        $candidates = array(
53
            'Locations' => $location,
54
            'Entities'  => $entity,
55
        );
56

    
57
        foreach ($candidates as $type => $candidate) {
58
            if ('N/A' === $candidate->getName()) {
59
                continue;
60
            }
61

    
62
            $completeName       = explode(" > ", $candidate->getField("completename"));
63
            // Ajout de "/" et de l'origine pour avoir le chemin complet.
64
            array_unshift($completeName, $type);
65
            array_unshift($completeName, "");
66
            $groupName          = implode("/", $completeName);
67
            $this->children[]   = new VigiloGroup($groupName);
68
        }
69

    
70
        $manufacturer = new Manufacturer();
71
        $manufacturer->getFromDB($this->item->fields["manufacturers_id"]);
72
        if ('N/A' !== $manufacturer->getName()) {
73
            $this->children[] = new VigiloGroup("/Manufacturers/" . $manufacturer->getName());
74
        }
75
    }
76

    
77
    protected function selectAddress()
78
    {
79
        static $address = null;
80

    
81
        if (null === $address && $this->agent) {
82
            $addresses = $this->agent->getIPs();
83
            if (count($addresses)) {
84
                $address = current($addresses);
85
            }
86
        }
87

    
88
        if (null === $address) {
89
            $address = $this->item->getName();
90
            foreach ($this->addresses as $addr) {
91
                if (!$addr->is_ipv4()) {
92
                    continue;
93
                }
94

    
95
                $textual = $addr->getTextual();
96
                if (is_string($textual)) {
97
                    $address = $textual;
98
                    break;
99
                }
100
            }
101
        }
102

    
103
        return $address;
104
    }
105

    
106
    protected function monitorNetworkInterfaces()
107
    {
108
        global $DB;
109

    
110
        $query = NetworkPort::getSQLRequestToSearchForItem(
111
            $this->item->getType(),
112
            $this->item->getID()
113
        );
114

    
115
        foreach ($DB->query($query) as $np) {
116
            $query2     = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
117
            $port       = new NetworkPort();
118
            $ethport    = new NetworkPortEthernet();
119

    
120
            $port->getFromDB($np['id']);
121
            if ($port->getName() == 'lo') {
122
                continue;
123
            }
124

    
125
            $label = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
126

    
127
            $this->children[] =
128
                        $test = new VigiloTest('Interface');
129
            $test['label']  = $label;
130
            $test['ifname'] = $port->getName();
131

    
132
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
133
            foreach ($ethport as $rowEthPort) {
134
                if ($rowEthPort['speed']) {
135
                    // La bande passante de l'interface est exprimée
136
                    // en Mbit/s dans GLPI et on la veut en bit/s dans Vigilo.
137
                    $test['max'] = $rowEthPort['speed'] << 20;
138
                    break;
139
                }
140
            }
141

    
142
            // Récupère la liste de toutes les adresses IP pour l'interface.
143
            // Elles serviront plus tard dans selectAddress() pour choisir
144
            // l'adresse IP la plus appropriée pour interroger ce réseau.
145
            foreach ($DB->query($query2) as $nn) {
146
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
147
                foreach ($DB->query($query3) as $ip) {
148
                    $addr = new IPAddress();
149
                    if ($addr->getFromDB($ip['id'])) {
150
                        $this->addresses[] = $addr;
151
                    }
152
                }
153
            }
154
        }
155
    }
156

    
157
    public function __toString()
158
    {
159
        $outXML = new DOMDocument();
160
        $outXML->preserveWhiteSpace = false;
161
        $outXML->formatOutput       = true;
162
        $outXML->loadXML(
163
            self::sprintf(
164
                '<?xml version="1.0"?>' .
165
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
166
                $this->item->getName(),
167
                $this->selectAddress(),
168
                "Servers",
169
                $this->children
170
            )
171
        );
172
        return $outXML->saveXML();
173
    }
174
}