Project

General

Profile

Revision bef4271a

IDbef4271aaef0661349bce2b7e296cfaa4d3a6a40
Parent 4b923a13
Child a5c9df40

Added by Thibault Louet over 7 years ago

Nouvelle structure des fichiers

Nouveau dossier Vigilo avec toute les classes. Hook et Setup modifiés
pour mettre à jour le fichier XML de l'ordinateur automatique dès que
l'un de ses élément a été modifié. Autoloader rajouté.

Change-Id: Iaffecfc3bd6aae191e0cb87fa1dc6a6da7956451
Reviewed-on: https://vigilo-dev.si.c-s.fr/review/2234
Tested-by: Francois POIROTTE <>
Reviewed-by: Francois POIROTTE <>

View differences:

Vigilo/VigiloArg.php
1
<?php
2

  
3
class VigiloArg extends VigiloXml
4
{
5
    protected $name;
6
    protected $values;
7

  
8
    public function __construct($name, $values)
9
    {
10
        if (is_array($values)) {
11
            $new_values = array();
12
            foreach ($values as $value) {
13
                if (is_string($value))
14
                    $new_values[] = new VigiloItem($value);
15
                else if (!is_a($value, 'VigiloItem'))
16
                    throw new \RuntimeException();
17
                else
18
                    $new_values[] = $value;
19
            }
20
            $values = $new_values;
21
        } else if (!is_string($values) && !is_int($values) &&
22
                   !is_bool($values) && !is_float($values))
23
            throw new \RuntimeException();
24
        else {
25
            $values = (string) $values;
26
        }
27

  
28
        $this->name = $name;
29
        $this->values = $values;
30
    }
31

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

  
37
    public function getValue()
38
    {
39
        if (is_string($this->values))
40
            return $this->values;
41
        return array_map('getValue', $this->values);
42
    }
43

  
44
    public function __toString()
45
    {
46
        return self::sprintf(
47
            '<arg name="%s">%s</arg>',
48
            $this->name,
49
            $this->values
50
        );
51
    }
52
}
Vigilo/VigiloAttribute.php
1
<?php
2

  
3
class VigiloAttribute extends VigiloXml
4
{
5
    protected $name;
6
    protected $value;
7

  
8
    public function __construct($name, $value)
9
    {
10
        $this->name = $name;
11
        $this->value = $value;
12
    }
13

  
14
    public function __toString()
15
    {
16
        return self::sprintf(
17
            '<attribute name="%s">%s</attribute>',
18
            $this->name,
19
            $this->value
20
        );
21
    }
22
}
Vigilo/VigiloGroup.php
1
<?php
2

  
3
class VigiloGroup extends VigiloXml
4
{
5
    protected $name;
6

  
7
    public function __construct($group)
8
    {
9
        $this->name = $group;
10
    }
11

  
12
    public function __toString()
13
    {
14
        return self::sprintf('<group>%s</group>', $this->name);
15
    }
16
}
Vigilo/VigiloHost.php
1
<?php
2

  
3
class VigiloHost extends VigiloXml
4
{
5
    protected $computer;
6
    protected $addresses;
7
    protected $ventilation;
8
    protected $children;
9
    protected $agent;
10

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

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

  
25
        $this->selectTemplates();
26
        $this->selectGroups();
27
        $this->monitorProcessor();
28
        $this->monitorMemory();
29
        $this->monitorNetworkInterfaces();
30
        $this->monitorSoftwares();
31
        $this->monitorPartitions();
32
    }
33

  
34
    public function getName()
35
    {
36
        return $this->computer->getName();
37
    }
38

  
39
    protected function selectTemplates()
40
    {
41
        $refs = array(
42
            "glpi_operatingsystems" => "operatingsystems_id",
43
            "glpi_operatingsystemversions" => "operatingsystemversions_id",
44
            "glpi_operatingsystemservicepacks" => "operatingsystemservicepacks_id",
45
        );
46

  
47
        $model = array();
48
        foreach ($refs as $table => $field) {
49
            $id = $this->computer->fields[$field];
50
            $value = Dropdown::getDropdownName($table, $id);
51
            if ($value !== "" && $value !== null && $value !== "&nbsp;" &&
52
                $value !== false && $value !== "-----") {
53
                $model[] = $value;
54
            }
55
        }
56

  
57
        if (!count($model))
58
            $model = "default";
59
        else
60
            $model = implode(" - ", $model);
61

  
62
        $this->children[] = new VigiloHostTemplate($model);
63
    }
64

  
65
    protected function selectGroups()
66
    {
67
        $location = new Location();
68
        $location->getFromDB($this->computer->fields["locations_id"]);
69
        $location = $location->getName();
70

  
71
        if (!$location)
72
            $location = "Servers";
73

  
74
        $this->children[] = new VigiloGroup($location);
75
    }
76

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

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

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

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

  
101
        return $address;
102
    }
103

  
104
    protected function monitorProcessor()
105
    {
106
        $this->children[] = new VigiloTest('CPU');
107
    }
108

  
109
    protected function monitorMemory()
110
    {
111
        global $DB;
112

  
113
        $total = 0;
114
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
115
            $this->computer->getType(),
116
            $this->computer->getID()
117
        );
118

  
119
        foreach ($DB->query($query) as $mem) {
120
            $memory = new Item_DeviceMemory();
121
            $memory->getFromDB($mem['id']);
122
            $total += $memory->fields['size'] * 1024 * 1024;
123
        }
124

  
125
        if ($total > 0)
126
            $this->children[] = new VigiloTest('RAM');
127
    }
128

  
129
    protected function monitorNetworkInterfaces()
130
    {
131
        global $DB;
132

  
133
        $query = NetworkPort::getSQLRequestToSearchForItem(
134
            $this->computer->getType(),
135
            $this->computer->getID()
136
        );
137

  
138
        foreach ($DB->query($query) as $np) {
139
            $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
140

  
141
            $port = new NetworkPort();
142
            $port->getFromDB($np['id']);
143
            if ($port->getName() == 'lo')
144
                continue;
145

  
146
            $args   = array();
147
            $label  = isset($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
148
            $args[] = new VigiloArg('label', $label);
149
            $args[] = new VigiloArg('name', $port->getName());
150
            // TODO: retrieve interface speed (from glpi_networkportethernets)
151
            $this->children[] = new VigiloTest('Interface', $args);
152

  
153
            // Retrieve all IP addresses associated with this interface.
154
            // This will be used later in selectAddress() to select
155
            // the most appropriate IP address to query this computer.
156
            foreach ($DB->query($query2) as $nn) {
157
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
158
                foreach ($DB->query($query3) as $ip) {
159
                    $addr = new IPAddress();
160
                    if ($addr->getFromDB($ip['id']))
161
                        $this->addresses[] = $addr;
162
                }
163
            }
164
        }
165
    }
166

  
167
    protected function monitorSoftwares()
168
    {
169
        
170
    }
171

  
172
    protected function monitorPartitions()
173
    {
174
        global $DB;
175

  
176
        $query = ComputerDisk::getSQLRequestToSearchForItem(
177
            $this->computer->getType(),
178
            $this->computer->getID()
179
        );
180

  
181
        foreach ($DB->query($query) as $cd) {
182
            $disk = new ComputerDisk();
183
            $disk->getFromDB($cd['id']);
184

  
185
            $args = array();
186
            $args[] = new VigiloArg('label', $disk->getName());
187
            $args[] = new VigiloArg('partname', $disk->fields['mountpoint']);
188
            $total = $disk->fields['totalsize'];
189
            if (!empty($total))
190
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
191
            $this->children[] = new VigiloTest('Partition', $args);
192
        }
193
    }
194

  
195
    public function __toString()
196
    {
197
        $outXML=new DOMdocument();
198
      	$outXML->preserveWhiteSpace=false;
199
     	$outXML->formatOutput=true;
200
     	$outXML->loadXML(self::sprintf(
201
            '<?xml version="1.0"?>' .
202
            '<host name="%s" address="%s" ventilation="%s">%s</host>',
203
            $this->computer->getName(),
204
            $this->selectAddress(),
205
            "Servers",
206
            $this->children
207
	    ));
208
	return $outXML->saveXML();	
209
	
210
    }
211
}
Vigilo/VigiloHost.php~
1
<?php
2

  
3
class VigiloHost extends VigiloXml
4
{
5
    protected $computer;
6
    protected $addresses;
7
    protected $ventilation;
8
    protected $children;
9
    protected $agent;
10

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

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

  
25
        $this->selectTemplates();
26
        $this->selectGroups();
27
        $this->monitorProcessor();
28
        $this->monitorMemory();
29
        $this->monitorNetworkInterfaces();
30
        $this->monitorSoftwares();
31
        $this->monitorPartitions();
32
    }
33

  
34
    public function getName()
35
    {
36
        return $this->computer->getName();
37
    }
38

  
39
    protected function selectTemplates()
40
    {
41
        $refs = array(
42
            "glpi_operatingsystems" => "operatingsystems_id",
43
            "glpi_operatingsystemversions" => "operatingsystemversions_id",
44
            "glpi_operatingsystemservicepacks" => "operatingsystemservicepacks_id",
45
        );
46

  
47
        $model = array();
48
        foreach ($refs as $table => $field) {
49
            $id = $this->computer->fields[$field];
50
            $value = Dropdown::getDropdownName($table, $id);
51
            if ($value !== "" && $value !== null && $value !== "&nbsp;" &&
52
                $value !== false && $value !== "-----") {
53
                $model[] = $value;
54
            }
55
        }
56

  
57
        if (!count($model))
58
            $model = "default";
59
        else
60
            $model = implode(" - ", $model);
61

  
62
        $this->children[] = new VigiloHostTemplate($model);
63
    }
64

  
65
    protected function selectGroups()
66
    {
67
        $location = new Location();
68
        $location->getFromDB($this->computer->fields["locations_id"]);
69
        $location = $location->getName();
70

  
71
        if (!$location)
72
            $location = "Servers";
73

  
74
        $this->children[] = new VigiloGroup($location);
75
    }
76

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

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

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

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

  
101
        return $address;
102
    }
103

  
104
    protected function monitorProcessor()
105
    {
106
        $this->children[] = new VigiloTest('CPU');
107
    }
108

  
109
    protected function monitorMemory()
110
    {
111
        global $DB;
112

  
113
        $total = 0;
114
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
115
            $this->computer->getType(),
116
            $this->computer->getID()
117
        );
118

  
119
        foreach ($DB->query($query) as $mem) {
120
            $memory = new Item_DeviceMemory();
121
            $memory->getFromDB($mem['id']);
122
            $total += $memory->fields['size'] * 1024 * 1024;
123
        }
124

  
125
        if ($total > 0)
126
            $this->children[] = new VigiloTest('RAM');
127
    }
128

  
129
    protected function monitorNetworkInterfaces()
130
    {
131
        global $DB;
132

  
133
        $query = NetworkPort::getSQLRequestToSearchForItem(
134
            $this->computer->getType(),
135
            $this->computer->getID()
136
        );
137

  
138
        foreach ($DB->query($query) as $np) {
139
            $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
140

  
141
            $port = new NetworkPort();
142
            $port->getFromDB($np['id']);
143
            if ($port->getName() == 'lo')
144
                continue;
145

  
146
            $args   = array();
147
            $label  = isset($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
148
            $args[] = new VigiloArg('label', $label);
149
            $args[] = new VigiloArg('name', $port->getName());
150
            // TODO: retrieve interface speed (from glpi_networkportethernets)
151
            $this->children[] = new VigiloTest('Interface', $args);
152

  
153
            // Retrieve all IP addresses associated with this interface.
154
            // This will be used later in selectAddress() to select
155
            // the most appropriate IP address to query this computer.
156
            foreach ($DB->query($query2) as $nn) {
157
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
158
                foreach ($DB->query($query3) as $ip) {
159
                    $addr = new IPAddress();
160
                    if ($addr->getFromDB($ip['id']))
161
                        $this->addresses[] = $addr;
162
                }
163
            }
164
        }
165
    }
166

  
167
    protected function monitorSoftwares()
168
    {
169
        
170
    }
171

  
172
    protected function monitorPartitions()
173
    {
174
        global $DB;
175

  
176
        $query = ComputerDisk::getSQLRequestToSearchForItem(
177
            $this->computer->getType(),
178
            $this->computer->getID()
179
        );
180

  
181
        foreach ($DB->query($query) as $cd) {
182
            $disk = new ComputerDisk();
183
            $disk->getFromDB($cd['id']);
184

  
185
            $args = array();
186
            $args[] = new VigiloArg('label', $disk->getName());
187
            $args[] = new VigiloArg('partname', $disk->fields['mountpoint']);
188
            $total = $disk->fields['totalsize'];
189
            if (!empty($total))
190
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
191
            $this->children[] = new VigiloTest('Partition', $args);
192
        }
193
    }
194

  
195
    public function __toString()
196
    {
197
        $outXML=new DOMdocument();
198
      	$outXML->preserveWhiteSpace=false;
199
     	$outXML->formatOutput=true;
200
     	$outXML->loadXML(self::sprintf(
201
            '<?xml version="1.0"?>' .
202
            '<host name="%s" address="%s" ventilation="%s">%s</host>',
203
            $this->computer->getName(),
204
            $this->selectAddress(),
205
            "Servers",
206
            $this->children
207
	    ));
208
	return $outXML->saveXML();	
209
	
210
    }
211
}
Vigilo/VigiloHostTemplate.php
1
<?php
2

  
3
class VigiloHostTemplate extends VigiloXml
4
{
5
    protected $name;
6

  
7
    public function __construct($tpl)
8
    {
9
        $this->name = $tpl;
10
    }
11

  
12
    public function __toString()
13
    {
14
        return self::sprintf('<template>%s</template>', $this->name);
15
    }
16
}
Vigilo/VigiloItem.php
1
<?php
2

  
3
class VigiloItem extends VigiloXml
4
{
5
    protected $value;
6

  
7
    public function __construct($value)
8
    {
9
        $this->value = $value;
10
    }
11

  
12
    public function getValue()
13
    {
14
        return $this->value;
15
    }
16

  
17
    public function __toString()
18
    {
19
        return self::sprintf('<item>%s</item>', $this->value);
20
    }
21
}
Vigilo/VigiloTest.php
1
<?php
2

  
3
class VigiloTest extends VigiloXml
4
{
5
    protected $name;
6
    protected $args;
7

  
8
    public function __construct($name, array $args = array())
9
    {
10
        $new_args = array();
11
        foreach ($args as $arg) {
12
            if (!is_a($arg, 'VigiloArg'))
13
                    throw new \RuntimeException();
14
            $new_args[$arg->getName()] = $arg;
15
        }
16

  
17
        $this->name = $name;
18
        $this->args = $new_args;
19
    }
20

  
21
    public function __toString()
22
    {
23
        return self::sprintf(
24
            '<test name="%s">%s</test>',
25
            $this->name,
26
            $this->args
27
        );
28
    }
29
}
30
 
Vigilo/VigiloXml.php
1
<?php
2

  
3
abstract class VigiloXml
4
{
5
    abstract public function __toString();
6

  
7
    public static function sprintf($s)
8
    {
9
        $args = func_get_args();
10
        array_shift($args); // pop $s
11

  
12
        $new_args = array();
13
        foreach ($args as $arg) {
14
            if (is_string($arg)) {
15
                $new_args[] = htmlspecialchars($arg, ENT_XML1 | ENT_QUOTES, "utf-8");
16
            } else if (is_array($arg)) {
17
                $acc = '';
18
                foreach ($arg as $sub) {
19
                    if (is_object($sub))
20
                        $acc .= (string) $sub;
21
                }
22
                $new_args[] = $acc;
23
            } else {
24
                $new_args[] = (string) $arg;
25
            }
26
        }
27

  
28
        return vsprintf($s, $new_args);
29
    }
30
}
31

  
autoloader.php
1
<?php
2

  
3
function vigilo_autoloader($class_name){
4
  if(strstr($class_name,'Vigilo')){
5
    require_once __DIR__ . '/Vigilo/' . $class_name . '.php';
6
  }
7
}
hook.php
1 1
<?php
2 2

  
3 3
//require_once(__DIR__ . DIRECTORY_SEPARATOR . '');
4

  
5
abstract class VigiloXml
6
{
7
    abstract public function __toString();
8

  
9
    public static function sprintf($s)
10
    {
11
        $args = func_get_args();
12
        array_shift($args); // pop $s
13

  
14
        $new_args = array();
15
        foreach ($args as $arg) {
16
            if (is_string($arg)) {
17
                $new_args[] = htmlspecialchars($arg, ENT_XML1 | ENT_QUOTES, "utf-8");
18
            } else if (is_array($arg)) {
19
                $acc = '';
20
                foreach ($arg as $sub) {
21
                    if (is_object($sub))
22
                        $acc .= (string) $sub;
23
                }
24
                $new_args[] = $acc;
25
            } else {
26
                $new_args[] = (string) $arg;
27
            }
28
        }
29

  
30
        return vsprintf($s, $new_args);
31
    }
32
}
33

  
34
class VigiloHostTemplate extends VigiloXml
35
{
36
    protected $name;
37

  
38
    public function __construct($tpl)
39
    {
40
        $this->name = $tpl;
41
    }
42

  
43
    public function __toString()
44
    {
45
        return self::sprintf('<template>%s</template>', $this->name);
46
    }
47
}
48

  
49
class VigiloGroup extends VigiloXml
50
{
51
    protected $name;
52

  
53
    public function __construct($group)
54
    {
55
        $this->name = $group;
56
    }
57

  
58
    public function __toString()
59
    {
60
        return self::sprintf('<group>%s</group>', $this->name);
61
    }
62
}
63

  
64
class VigiloAttribute extends VigiloXml
65
{
66
    protected $name;
67
    protected $value;
68

  
69
    public function __construct($name, $value)
70
    {
71
        $this->name = $name;
72
        $this->value = $value;
73
    }
74

  
75
    public function __toString()
76
    {
77
        return self::sprintf(
78
            '<attribute name="%s">%s</attribute>',
79
            $this->name,
80
            $this->value
81
        );
82
    }
83
}
84

  
85
class VigiloItem extends VigiloXml
86
{
87
    protected $value;
88

  
89
    public function __construct($value)
90
    {
91
        $this->value = $value;
92
    }
93

  
94
    public function getValue()
95
    {
96
        return $this->value;
97
    }
98

  
99
    public function __toString()
100
    {
101
        return self::sprintf('<item>%s</item>', $this->value);
102
    }
103
}
104

  
105
class VigiloArg extends VigiloXml
106
{
107
    protected $name;
108
    protected $values;
109

  
110
    public function __construct($name, $values)
111
    {
112
        if (is_array($values)) {
113
            $new_values = array();
114
            foreach ($values as $value) {
115
                if (is_string($value))
116
                    $new_values[] = new VigiloItem($value);
117
                else if (!is_a($value, 'VigiloItem'))
118
                    throw new \RuntimeException();
119
                else
120
                    $new_values[] = $value;
121
            }
122
            $values = $new_values;
123
        } else if (!is_string($values) && !is_int($values) &&
124
                   !is_bool($values) && !is_float($values))
125
            throw new \RuntimeException();
126
        else {
127
            $values = (string) $values;
128
        }
129

  
130
        $this->name = $name;
131
        $this->values = $values;
132
    }
133

  
134
    public function getName()
135
    {
136
        return $this->name;
137
    }
138

  
139
    public function getValue()
140
    {
141
        if (is_string($this->values))
142
            return $this->values;
143
        return array_map('getValue', $this->values);
144
    }
145

  
146
    public function __toString()
147
    {
148
        return self::sprintf(
149
            '<arg name="%s">%s</arg>',
150
            $this->name,
151
            $this->values
152
        );
153
    }
154
}
155

  
156
class VigiloTest extends VigiloXml
157
{
158
    protected $name;
159
    protected $args;
160

  
161
    public function __construct($name, array $args = array())
162
    {
163
        $new_args = array();
164
        foreach ($args as $arg) {
165
            if (!is_a($arg, 'VigiloArg'))
166
                    throw new \RuntimeException();
167
            $new_args[$arg->getName()] = $arg;
168
        }
169

  
170
        $this->name = $name;
171
        $this->args = $new_args;
172
    }
173

  
174
    public function __toString()
175
    {
176
        return self::sprintf(
177
            '<test name="%s">%s</test>',
178
            $this->name,
179
            $this->args
180
        );
181
    }
182
}
183

  
184
class VigiloHost extends VigiloXml
185
{
186
    protected $computer;
187
    protected $addresses;
188
    protected $ventilation;
189
    protected $children;
190
    protected $agent;
191

  
192
    public function __construct($computer)
193
    {
194
        $this->agent        = null;
195
        $this->ventilation  = "Servers";
196
        $this->computer     = $computer;
197
        $this->addresses    = array();
198
        $this->children     = array();
199

  
200
        if (class_exists('PluginFusioninventoryAgent')) {
201
            $agent = new PluginFusioninventoryAgent();
202
            if ($agent->getAgentWithComputerid($this->computer->getID()) !== false)
203
                $this->agent = $agent;
204
        }
205

  
206
        $this->selectTemplates();
207
        $this->selectGroups();
208
        $this->monitorProcessor();
209
        $this->monitorMemory();
210
        $this->monitorNetworkInterfaces();
211
        $this->monitorSoftwares();
212
        $this->monitorPartitions();
213
    }
214

  
215
    public function getName()
216
    {
217
        return $this->computer->getName();
218
    }
219

  
220
    protected function selectTemplates()
221
    {
222
        $refs = array(
223
            "glpi_operatingsystems" => "operatingsystems_id",
224
            "glpi_operatingsystemversions" => "operatingsystemversions_id",
225
            "glpi_operatingsystemservicepacks" => "operatingsystemservicepacks_id",
226
        );
227

  
228
        $model = array();
229
        foreach ($refs as $table => $field) {
230
            $id = $this->computer->fields[$field];
231
            $value = Dropdown::getDropdownName($table, $id);
232
            if ($value !== "" && $value !== null && $value !== "&nbsp;" &&
233
                $value !== false && $value !== "-----") {
234
                $model[] = $value;
235
            }
236
        }
237

  
238
        if (!count($model))
239
            $model = "default";
240
        else
241
            $model = implode(" - ", $model);
242

  
243
        $this->children[] = new VigiloHostTemplate($model);
244
    }
245

  
246
    protected function selectGroups()
247
    {
248
        $location = new Location();
249
        $location->getFromDB($this->computer->fields["locations_id"]);
250
        $location = $location->getName();
251

  
252
        if (!$location)
253
            $location = "Servers";
254

  
255
        $this->children[] = new VigiloGroup($location);
256
    }
257

  
258
    protected function selectAddress()
259
    {
260
        static $address = null;
261

  
262
        if ($address === null && $this->agent) {
263
            $addresses = $this->agent->getIPs();
264
            if (count($addresses))
265
                $address = current($addresses);
266
        }
267

  
268
        if ($address === null) {
269
            $address = $this->$computer->getName();
270
            foreach ($this->addresses as $addr) {
271
                if (!$addr->is_ipv4())
272
                    continue;
273

  
274
                $textual = $addr->getTextual();
275
                if (is_string($textual)) {
276
                    $address = $textual;
277
                    break;
278
                }
279
            }
280
        }
281

  
282
        return $address;
283
    }
284

  
285
    protected function monitorProcessor()
286
    {
287
        $this->children[] = new VigiloTest('CPU');
288
    }
289

  
290
    protected function monitorMemory()
291
    {
292
        global $DB;
293

  
294
        $total = 0;
295
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
296
            $this->computer->getType(),
297
            $this->computer->getID()
298
        );
299

  
300
        foreach ($DB->query($query) as $mem) {
301
            $memory = new Item_DeviceMemory();
302
            $memory->getFromDB($mem['id']);
303
            $total += $memory->fields['size'] * 1024 * 1024;
304
        }
305

  
306
        if ($total > 0)
307
            $this->children[] = new VigiloTest('RAM');
308
    }
309

  
310
    protected function monitorNetworkInterfaces()
311
    {
312
        global $DB;
313

  
314
        $query = NetworkPort::getSQLRequestToSearchForItem(
315
            $this->computer->getType(),
316
            $this->computer->getID()
317
        );
318

  
319
        foreach ($DB->query($query) as $np) {
320
            $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
321

  
322
            $port = new NetworkPort();
323
            $port->getFromDB($np['id']);
324
            if ($port->getName() == 'lo')
325
                continue;
326

  
327
            $args   = array();
328
            $label  = isset($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
329
            $args[] = new VigiloArg('label', $label);
330
            $args[] = new VigiloArg('name', $port->getName());
331
            // TODO: retrieve interface speed (from glpi_networkportethernets)
332
            $this->children[] = new VigiloTest('Interface', $args);
333

  
334
            // Retrieve all IP addresses associated with this interface.
335
            // This will be used later in selectAddress() to select
336
            // the most appropriate IP address to query this computer.
337
            foreach ($DB->query($query2) as $nn) {
338
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
339
                foreach ($DB->query($query3) as $ip) {
340
                    $addr = new IPAddress();
341
                    if ($addr->getFromDB($ip['id']))
342
                        $this->addresses[] = $addr;
343
                }
344
            }
345
        }
346
    }
347

  
348
    protected function monitorSoftwares()
349
    {
350
        
351
    }
352

  
353
    protected function monitorPartitions()
354
    {
355
        global $DB;
356

  
357
        $query = ComputerDisk::getSQLRequestToSearchForItem(
358
            $this->computer->getType(),
359
            $this->computer->getID()
360
        );
361

  
362
        foreach ($DB->query($query) as $cd) {
363
            $disk = new ComputerDisk();
364
            $disk->getFromDB($cd['id']);
365

  
366
            $args = array();
367
            $args[] = new VigiloArg('label', $disk->getName());
368
            $args[] = new VigiloArg('partname', $disk->fields['mountpoint']);
369
            $total = $disk->fields['totalsize'];
370
            if (!empty($total))
371
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
372
            $this->children[] = new VigiloTest('Partition', $args);
373
        }
374
    }
375

  
376
    public function __toString()
377
    {
378
        return self::sprintf(
379
            '<?xml version="1.0"?>' .
380
            '<host name="%s" address="%s" ventilation="%s">%s</host>',
381
            $this->computer->getName(),
382
            $this->selectAddress(),
383
            "Servers",
384
            $this->children
385
        );
386
    }
387
}
388

  
4
require __DIR__ . '/autoloader.php';
389 5
class VigiloHooks
390 6
{
391 7
    private $confdir;
392 8

  
393 9
    public function __construct($confdir="/etc/vigilo/vigiconf/conf.d")
394 10
    {
11
        spl_autoload_register('vigilo_autoloader');
395 12
        $this->confdir = $confdir;
396 13
    }
397 14

  
......
435 52
        $dirs = array($this->confdir, "hosts", "managed", $host . ".xml");
436 53
        unlink(implode(DIRECTORY_SEPARATOR, $dirs));
437 54
    }
55

  
56
    public function manageDisks($disk){
57
      
58
      global $DB;
59
      
60
      $query="SELECT computers_id FROM glpi_" . strtolower($disks->getType()) . "s WHERE id=" . $disks->getID() . ";";
61
      foreach($DB->query($query) as $id){
62
	$computer= new Computer();
63
	$computer->getFromDB($id['id']);
64
	$this->update($computer);
65
      }
66
    }
67

  
68
    public function manageNetworks($network){
69
      
70
      global $DB;
71

  
72
      $query="SELECT computers_id FROM glpi_computers WHERE networks_id=" . $network->getID() . ";";
73
      foreach($DB->query($query) as $id){
74
	$computer=new Computer();
75
	$computer->getFromDB($id['id']);
76
	$this->update($computer);
77
      }
78
    }
438 79
}
439 80

  
setup.php
10 10
    $hookObj    =  new VigiloHooks();
11 11

  
12 12
    $hooks['csrf_compliant'][$p]        = true;
13
    $hooks['item_add'][$p]              = array("Computer" => array($hookObj, "add"));
14
    $hooks['item_update'][$p]           = array("Computer" => array($hookObj, "update"));
15
    $hooks['item_purge'][$p]            = array("Computer" => array($hookObj, "delete"));
16
    $hooks['item_delete'][$p]           = array("Computer" => array($hookObj, "delete"));
17
    $hooks['item_restore'][$p]          = array("Computer" => array($hookObj, "add"));
13
    $hooks['item_add'][$p]              = array("Computer" => array($hookObj, "add"),
14
						"ComputerDisk" => array($hookObj),"manageDisks",
15
						"Network" => array($hookObj),"manageNetworks");
16
    $hooks['item_update'][$p]           = array("Computer" => array($hookObj, "update"),
17
						"ComputerDisk" => array($hookObj),"manageDisks",
18
						"Network" => array($hookObj),"manageNetworks");
19
    $hooks['item_purge'][$p]            = array("Computer" => array($hookObj, "delete"),
20
						"ComputerDisk" => array($hookObj),"manageDisks",
21
						"Network" => array($hookObj),"manageNetworks");
22
    $hooks['item_delete'][$p]           = array("Computer" => array($hookObj, "delete"),
23
						"ComputerDisk" => array($hookObj),"manageDisks",
24
						"Network" => array($hookObj),"manageNetworks");
25
    $hooks['item_restore'][$p]          = array("Computer" => array($hookObj, "add"),
26
						"ComputerDisk" => array($hookObj),"manageDisks",
27
						"Network" => array($hookObj),"manageNetworks");
18 28
    $hooks["menu_toadd"][$p]['plugins'] = 'PluginVigiloMenu';
19 29
    $hooks['config_page'][$p]           = 'front/menu.php?itemtype=vigilo';
20 30
}

Also available in: Unified diff