Project

General

Profile

Revision 166be9d2

ID166be9d2fe6802379629f4459aab989de6904710
Parent 0e8eed1f
Child 401230e2

Added by Francois POIROTTE about 7 years ago

Gros refactoring

- Déplacement du code vers "src/plugins/vigilo/" pour permettre d'avoir
une installation locale de GLPI sous "src/".
- Correction des dépendances dans composer.json.
- Suppression de certaines classes qui ne correspondaient pas à des
balises XML côté Vigilo du dossier "src/plugins/vigilo/Vigilo".
- Stockage du nom du template plutôt qu'un ID volatile.
- Utilisation d'une table privée (plutôt qu'un hack dans les tables
natives de GLPI).
- Possibilité d'activer ou non le mode debug depuis l'IHM.
- Affichage d'un message pour indiquer si la conf doit être redéployée
ou non.

Change-Id: I3c5f025e57aff8bc196726fa47956f04c3034ce9
Reviewed-on: https://vigilo-dev.si.c-s.fr/review/2373
Tested-by: Build system <>
Reviewed-by: Francois POIROTTE <>

View differences:

.gitignore
29 29
src/twisted/plugins/dropin.cache
30 30

  
31 31
/vendor/
32
/src/.*
33
/src/*.*
34
/src/ajax/
35
/src/config/
36
/src/css/
37
/src/files/
38
/src/front/
39
/src/inc/
40
/src/install/
41
/src/lib/
42
/src/locales/
43
/src/pics/
44
/src/plugins/remove.txt
45
/src/scripts/
46
/src/tests/
47
/src/tools/
48
/src/vendor/
Makefile
7 7
install: install_pkg
8 8

  
9 9
install_pkg: $(INFILES)
10
	-mkdir -p $(DESTDIR)$(DATADIR)/$(NAME)/plugins/vigilo
11
	cp -pr src/* $(DESTDIR)$(DATADIR)/$(NAME)/plugins/vigilo/
10
	-mkdir -p $(DESTDIR)$(DATADIR)/$(NAME)/plugins/
11
	cp -pr src/plugins/vigilo $(DESTDIR)$(DATADIR)/$(NAME)/plugins/
12 12

  
13 13
clean: clean_common
14 14

  
composer.json
3 3
    "description": "Vigilo integration plugin for GLPI",
4 4
    "type": "library",
5 5
    "license": "GPL-2.0+",
6
    "require": {},
6
    "require": {
7
        "ext-dom": "*",
8
        "ext-pcre": "*"
9
    },
7 10
    "require-dev": {
8
        "squizlabs/php_codesniffer": "*"
11
        "squizlabs/php_codesniffer": "< 3.0.0"
9 12
    }
10 13
}
composer.lock
4 4
        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5 5
        "This file is @generated automatically"
6 6
    ],
7
    "content-hash": "f40b06143356be9f390e135a8e99434a",
7
    "content-hash": "e526e0f96c47d17decbf8bd95d6b5372",
8 8
    "packages": [],
9 9
    "packages-dev": [
10 10
        {
......
91 91
    "stability-flags": [],
92 92
    "prefer-stable": false,
93 93
    "prefer-lowest": false,
94
    "platform": [],
94
    "platform": {
95
        "ext-dom": "*",
96
        "ext-pcre": "*"
97
    },
95 98
    "platform-dev": []
96 99
}
phpcs.xml
1 1
<?xml version="1.0"?>
2 2
<ruleset name="pssht">
3 3
    <description>Coding standard for Vigilo.</description>
4
    <file>src/</file>
4
    <file>src/plugins/vigilo</file>
5 5
    <arg value="vp"/>
6 6
    <rule ref="PSR2">
7 7
        <!--
src/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
                } elseif (!is_a($value, 'VigiloItem')) {
16
                    throw new \RuntimeException();
17
                } else {
18
                    $new_values[] = $value;
19
                }
20
            }
21
            $values = $new_values;
22
        } elseif (!is_string($values) && !is_int($values)
23
            && !is_bool($values) && !is_float($values)) {
24
            throw new \RuntimeException();
25
        } else {
26
            $values = (string) $values;
27
        }
28

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

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

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

  
46
    public function __toString()
47
    {
48
        return self::sprintf(
49
            '<arg name="%s">%s</arg>',
50
            $this->name,
51
            $this->values
52
        );
53
    }
54
}
src/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
}
src/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
}
src/Vigilo/VigiloGroups.php
1
<?php
2

  
3
class VigiloGroups extends VigiloXml
4
{
5
    protected $name;
6
    protected $groups;
7

  
8
    public function __construct($name)
9
    {
10
        $this->name     = $name;
11
        $this->groups   = array();
12
    }
13
    
14
    public function getName()
15
    {
16
        return $this->name;
17
    }
18

  
19
    public function addSubGroup(VigiloGroups $subGroup)
20
    {
21
        $this->groups[$subGroup->getName()] = $subGroup;
22
    }
23

  
24
    public function __toString()
25
    {
26
        return self::sprintf(
27
            '<group name="%s">%s</group>',
28
            $this->name,
29
            $this->groups
30
        );
31
    }
32
}
src/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

  
26
        $this->selectTemplates();
27
        $this->selectGroups();
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
        $template_name = $this->computer->getField("template_name");
42

  
43
        if ($template_name && "N/A" !== $template_name) {
44
            $this->children[] = new VigiloHostTemplate($this->computer->getField("template_name"));
45
        }
46

  
47
        $template_number = $this->computer->getField("vigilo_template");
48
        if ('0' !== $template_number && 'N/A' !== $template_number) {
49
            $common_dbtm = new CommonDBTM();
50
            $template_name = PluginVigiloVigiloTemplate::getVigiloTemplateNameByID($template_number);
51
            $this->children[] = new VigiloHostTemplate($template_name);
52
        } elseif (empty($this->children)) {
53
            $template_name = "default";
54
            $this->children[] = new VigiloHostTemplate($template_name);
55
        }
56
    }
57

  
58
    protected function selectGroups()
59
    {
60
        $location = new Location();
61
        $location->getFromDB($this->computer->fields["locations_id"]);
62
        if ('N/A' != $location->getName()) {
63
            $locationCompleteName   = explode(" > ", $location->getField("completename"));
64
            $locationRealName       = implode("/", $locationCompleteName);
65
            $this->children[]       = new VigiloGroup($locationRealName);
66
        }
67

  
68
        $entity = new Entity();
69
        $entity->getFromDB($this->computer->fields["entities_id"]);
70
        if ('N/A' != $entity->getName()) {
71
            $entityCompleteName = explode(" > ", $entity->getField("completename"));
72
            $entityRealName     = implode("/", $entityCompleteName);
73
            $this->children[]   = new VigiloGroup($entityRealName);
74
        }
75

  
76
        $manufacturer = new Manufacturer();
77
        $manufacturer->getFromDB($this->computer->fields["manufacturers_id"]);
78
        if ('N/A' != $manufacturer->getName()) {
79
            $this->children[] = new VigiloGroup($manufacturer->getName());
80
        }
81
    }
82

  
83
    protected function selectAddress()
84
    {
85
        static $address = null;
86

  
87
        if (null !== $address && $this->agent) {
88
            $addresses = $this->agent->getIPs();
89
            if (count($addresses)) {
90
                $address = current($addresses);
91
            }
92
        }
93

  
94
        if (null !== $address) {
95
            $address = $this->computer->getName();
96
            foreach ($this->addresses as $addr) {
97
                if (!$addr->is_ipv4()) {
98
                    continue;
99
                }
100

  
101
                $textual = $addr->getTextual();
102
                if (is_string($textual)) {
103
                    $address = $textual;
104
                    break;
105
                }
106
            }
107
        }
108

  
109
        return $address;
110
    }
111

  
112
    protected function monitorMemory()
113
    {
114
        global $DB;
115

  
116
        $total = 0;
117
        $query = Item_DeviceMemory::getSQLRequestToSearchForItem(
118
            $this->computer->getType(),
119
            $this->computer->getID()
120
        );
121

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

  
128
        if ($total > 0) {
129
            $this->children[] = new VigiloTest('RAM');
130
        }
131
    }
132

  
133
    protected function monitorNetworkInterfaces()
134
    {
135
        global $DB;
136
        $query = NetworkPort::getSQLRequestToSearchForItem(
137
            $this->computer->getType(),
138
            $this->computer->getID()
139
        );
140

  
141
        foreach ($DB->query($query) as $np) {
142
            $query2     = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
143
            $port       = new NetworkPort();
144
            $ethport    = new NetworkPortEthernet();
145
            $port->getFromDB($np['id']);
146
            if ('lo' == $port->getName()) {
147
                continue;
148
            }
149

  
150
            $args       = array();
151
            $label      = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
152
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
153
            foreach ($ethport as $rowEthPort) {
154
                if ($rowEthPort['speed']) {
155
                    $args[] = new VigiloArg('max', $rowEthPort['speed']);
156
                    break;
157
                }
158
            }
159
            $args[] = new VigiloArg('label', $label);
160
            $args[] = new VigiloArg('ifname', $port->getName());
161
            $this->children[] = new VigiloTest('Interface', $args);
162

  
163
            // Retrieve all IP addresses associated with this interface.
164
            // This will be used later in selectAddress() to select
165
            // the most appropriate IP address to query this computer.
166
            foreach ($DB->query($query2) as $nn) {
167
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
168
                foreach ($DB->query($query3) as $ip) {
169
                    $addr = new IPAddress();
170
                    if ($addr->getFromDB($ip['id'])) {
171
                        $this->addresses[] = $addr;
172
                    }
173
                }
174
            }
175
        }
176
    }
177

  
178
    protected function monitorSoftwares()
179
    {
180
        $listOfTest = new VigiloTestSoftware($this->computer);
181
        $computerSoftwareVersion = new Computer_SoftwareVersion();
182
        $ids = $computerSoftwareVersion->find('computers_id=' . $this->computer->getID());
183
        foreach ($ids as $id) {
184
            if ($id['softwareversions_id']) {
185
                $softwareVersion = new SoftwareVersion();
186
                $ids2 = $softwareVersion->find('id=' . $id['softwareversions_id']);
187
                foreach ($ids2 as $id2) {
188
                    if ($id2['softwares_id']) {
189
                        $software = new Software();
190
                        $software->getFromDB($id2['softwares_id']);
191
                        $listOfTest->addRelevantTestWith($software->getName());
192
                    }
193
                }
194
            }
195
        }
196
        foreach ($listOfTest->getTable() as $test) {
197
             $this->children[] = $test;
198
        }
199
    }
200

  
201
    protected function monitorPartitions()
202
    {
203
        global $DB;
204

  
205
        $query = ComputerDisk::getSQLRequestToSearchForItem(
206
            $this->computer->getType(),
207
            $this->computer->getID()
208
        );
209

  
210
        foreach ($DB->query($query) as $cd) {
211
            $disk = new ComputerDisk();
212
            $disk->getFromDB($cd['id']);
213

  
214
            $args = array();
215
            $args[] = new VigiloArg('label', $disk->getName());
216
            $args[] = new VigiloArg('partname', $disk->fields['mountpoint']);
217
            $total = $disk->fields['totalsize'];
218
            if (!empty($total)) {
219
                $args[] = new VigiloArg('max', $total * 1024 * 1024);
220
            }
221
            $this->children[] = new VigiloTest('Partition', $args);
222
        }
223
    }
224

  
225
    public function __toString()
226
    {
227
        $outXML = new DOMDocument();
228
        $outXML->preserveWhiteSpace = false;
229
        $outXML->formatOutput       = true;
230
        $outXML->loadXML(
231
            self::sprintf(
232
                '<?xml version="1.0"?>' .
233
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
234
                $this->computer->getName(),
235
                $this->selectAddress(),
236
                "Servers",
237
                $this->children
238
            )
239
        );
240
        return $outXML->saveXML();
241
    }
242
}
src/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
}
src/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
}
src/Vigilo/VigiloLocation.php
1
<?php
2

  
3
class VigiloLocation extends VigiloXml
4
{
5
    protected $childrenLocation;
6
    protected $childrenEntity;
7
    protected $childrenManufacturer;
8

  
9
    public function __construct()
10
    {
11
        $this->childrenLocation     = array();
12
        $this->childrenEntity       = array();
13
        $this->childrenManufacturer = array();
14
        $this->selectLocations();
15
        $this->selectEntities();
16
        $this->selectManufacturers();
17
    }
18

  
19
    protected function selectManufacturers()
20
    {
21
        $manufacturers = new Manufacturer();
22
        $manufacturers = $manufacturers->find();
23
        foreach ($manufacturers as $manufacturer) {
24
            $this->childrenManufacturer[] = new VigiloGroups($manufacturer["name"]);
25
        }
26
    }
27

  
28
    protected function selectEntities()
29
    {
30
        $entities   = new Entity();
31
        $entities   = $entities->find("", "completename");
32
        $ancestors  = array();
33
        foreach ($entities as $entity) {
34
            $currentLevel = $entity["level"];
35
            if ($currentLevel == 1 && isset($ancestors[1])) {
36
                $this->childrenEntity[] = $ancestors[1];
37
            }
38
            $tempEntity = new VigiloGroups($entity["name"]);
39
            $ancestors[$currentLevel] = $tempEntity;
40
            if ($currentLevel != 1) {
41
                $ancestors[$currentLevel - 1]->addSubGroup($tempEntity);
42
            }
43
        }
44
        $this->childrenEntity[] = $ancestors[1];
45
    }
46

  
47
    protected function selectLocations()
48
    {
49
        $locations = new Location();
50
        $locations = $locations->find("", "completename");
51
        $ancestors = array();
52
        foreach ($locations as $location) {
53
            $currentLevel = $location["level"];
54
            if ($currentLevel == 1 && isset($ancestors[1])) {
55
                $this->childrenLocation[] = $ancestors[1];
56
            }
57
            $tempLocation = new VigiloGroups($location["name"]);
58
            $ancestors[$currentLevel] = $tempLocation;
59
            if ($currentLevel != 1) {
60
                $ancestors[$currentLevel - 1]->addSubGroup($tempLocation);
61
            }
62
        }
63
        $this->childrenLocation[] = $ancestors[1];
64
    }
65
  
66
    public function __toString()
67
    {
68
        $outXML = new DOMDocument();
69
        $outXML->preserveWhiteSpace = false;
70
        $outXML->formatOutput       = true;
71
        $outXML->loadXML(
72
            self::sprintf(
73
                '<groups>
74
     <group name="Locations"> %s </group>
75
     <group name="Entities"> %s </group>
76
     <group name="Manufacturers"> %s </group>
77
     </groups>',
78
                $this->childrenLocation,
79
                $this->childrenEntity,
80
                $this->childrenManufacturer
81
            )
82
        );
83
        return $outXML->saveXML();
84
    }
85
}
src/Vigilo/VigiloNetworkEquipment.php
1
<?php
2

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

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

  
19
        if (class_exists('PluginFusioninventoryAgent')) {
20
            $agent = new PluginFusioninventoryAgent();
21
            if ($agent->getAgentWithComputerid($this->network->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->network->getName();
34
    }
35

  
36
    protected function selectTemplates()
37
    {
38
        $template_name = $this->network->getField("template_name");
39

  
40
        if ($template_name !== "N/A") {
41
            $this->children[] = new VigiloHostTemplate($this->network->getField("template_name"));
42
        }
43
    }
44

  
45
    protected function selectGroups()
46
    {
47
        $location = new Location();
48
        $location->getFromDB($this->network->fields["locations_id"]);
49
        if ('N/A' !== $location->getName()) {
50
            $locationCompleteName   = explode(" > ", $location->getField("completename"));
51
            $locationRealName       = implode("/", $locationCompleteName);
52
            $this->children[]       = new VigiloGroup($locationRealName);
53
        }
54

  
55
        $entity = new Entity();
56
        $entity->getFromDB($this->network->fields["entities_id"]);
57
        if ('N/A' !== $entity->getName()) {
58
            $entityCompleteName = explode(" > ", $entity->getField("completename"));
59
            $entityRealName     = implode("/", $entityCompleteName);
60
            $this->children[]   = new VigiloGroup($entityRealName);
61
        }
62

  
63
        $manufacturer = new Manufacturer();
64
        $manufacturer->getFromDB($this->network->fields["manufacturers_id"]);
65
        if ('N/A' !== $manufacturer->getName()) {
66
            $this->children[] = new VigiloGroup($manufacturer->getName());
67
        }
68
    }
69

  
70
    protected function selectAddress()
71
    {
72
        static $address = null;
73

  
74
        if ($address === null && $this->agent) {
75
            $addresses = $this->agent->getIPs();
76
            if (count($addresses)) {
77
                $address = current($addresses);
78
            }
79
        }
80

  
81
        if ($address === null) {
82
            $address = $this->network->getName();
83
            foreach ($this->addresses as $addr) {
84
                if (!$addr->is_ipv4()) {
85
                    continue;
86
                }
87

  
88
                $textual = $addr->getTextual();
89
                if (is_string($textual)) {
90
                    $address = $textual;
91
                    break;
92
                }
93
            }
94
        }
95

  
96
        return $address;
97
    }
98

  
99
    protected function monitorNetworkInterfaces()
100
    {
101
        global $DB;
102
        $query = NetworkPort::getSQLRequestToSearchForItem(
103
            $this->network->getType(),
104
            $this->network->getID()
105
        );
106

  
107
        foreach ($DB->query($query) as $np) {
108
            $query2     = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
109
            $port       = new NetworkPort();
110
            $ethport    = new NetworkPortEthernet();
111
            $port->getFromDB($np['id']);
112
            if ($port->getName() == 'lo') {
113
                continue;
114
            }
115

  
116
            $args       = array();
117
            $label      = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
118
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
119
            foreach ($ethport as $rowEthPort) {
120
                if ($rowEthPort['speed']) {
121
                    $args[] = new VigiloArg('max', $rowEthPort['speed']);
122
                    break;
123
                }
124
            }
125
            $args[] = new VigiloArg('label', $label);
126
            $args[] = new VigiloArg('ifname', $port->getName());
127
            $this->children[] = new VigiloTest('Interface', $args);
128

  
129
            // Retrieve all IP addresses associated with this interface.
130
            // This will be used later in selectAddress() to select
131
            // the most appropriate IP address to query this network.
132
            foreach ($DB->query($query2) as $nn) {
133
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
134
                foreach ($DB->query($query3) as $ip) {
135
                    $addr = new IPAddress();
136
                    if ($addr->getFromDB($ip['id'])) {
137
                        $this->addresses[] = $addr;
138
                    }
139
                }
140
            }
141
        }
142
    }
143

  
144
    public function __toString()
145
    {
146
        $outXML = new DOMDocument();
147
        $outXML->preserveWhiteSpace = false;
148
        $outXML->formatOutput       = true;
149
        $outXML->loadXML(
150
            self::sprintf(
151
                '<?xml version="1.0"?>' .
152
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
153
                $this->network->getName(),
154
                $this->selectAddress(),
155
                "Servers",
156
                $this->children
157
            )
158
        );
159
        return $outXML->saveXML();
160
    }
161
}
src/Vigilo/VigiloPrinter.php
1
<?php
2

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

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

  
19
        if (class_exists('PluginFusioninventoryAgent')) {
20
            $agent = new PluginFusioninventoryAgent();
21
            if ($agent->getAgentWithComputerid($this->network->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->network->getName();
34
    }
35

  
36
    protected function selectTemplates()
37
    {
38
        $template_name = $this->network->getField("template_name");
39

  
40
        if ($template_name !== "N/A") {
41
            $this->children[] = new VigiloHostTemplate($this->network->getField("template_name"));
42
        }
43
    }
44

  
45
    protected function selectGroups()
46
    {
47
        $location = new Location();
48
        $location->getFromDB($this->network->fields["locations_id"]);
49
        if ('N/A' !== $location->getName()) {
50
            $locationCompleteName   = explode(" > ", $location->getField("completename"));
51
            $locationRealName       = implode("/", $locationCompleteName);
52
            $this->children[]       = new VigiloGroup($locationRealName);
53
        }
54

  
55
        $entity = new Entity();
56
        $entity->getFromDB($this->network->fields["entities_id"]);
57
        if ('N/A' !== $entity->getName()) {
58
            $entityCompleteName = explode(" > ", $entity->getField("completename"));
59
            $entityRealName     = implode("/", $entityCompleteName);
60
            $this->children[]   = new VigiloGroup($entityRealName);
61
        }
62

  
63
        $manufacturer = new Manufacturer();
64
        $manufacturer->getFromDB($this->network->fields["manufacturers_id"]);
65
        if ('N/A' !== $manufacturer->getName()) {
66
            $this->children[] = new VigiloGroup($manufacturer->getName());
67
        }
68
    }
69

  
70
    protected function selectAddress()
71
    {
72
        static $address = null;
73

  
74
        if (null === $address && $this->agent) {
75
            $addresses = $this->agent->getIPs();
76
            if (count($addresses)) {
77
                $address = current($addresses);
78
            }
79
        }
80

  
81
        if (null === $address) {
82
            $address = $this->network->getName();
83
            foreach ($this->addresses as $addr) {
84
                if (!$addr->is_ipv4()) {
85
                    continue;
86
                }
87

  
88
                $textual = $addr->getTextual();
89
                if (is_string($textual)) {
90
                    $address = $textual;
91
                    break;
92
                }
93
            }
94
        }
95

  
96
        return $address;
97
    }
98

  
99
    protected function monitorNetworkInterfaces()
100
    {
101
        global $DB;
102
        $query = NetworkPort::getSQLRequestToSearchForItem(
103
            $this->network->getType(),
104
            $this->network->getID()
105
        );
106

  
107
        foreach ($DB->query($query) as $np) {
108
            $query2 = NetworkName::getSQLRequestToSearchForItem("NetworkPort", $np['id']);
109
            $port = new NetworkPort();
110
            $ethport = new NetworkPortEthernet();
111
            $port->getFromDB($np['id']);
112
            if ($port->getName() == 'lo') {
113
                continue;
114
            }
115

  
116
            $args       = array();
117
            $label      = !empty($port->fields['comment']) ? $port->fields['comment'] : $port->getName();
118
            $ethport    = $ethport->find('networkports_id=' . $np['id']);
119
            foreach ($ethport as $rowEthPort) {
120
                if ($rowEthPort['speed']) {
121
                    $args[] = new VigiloArg('max', $rowEthPort['speed']);
122
                    break;
123
                }
124
            }
125
            $args[] = new VigiloArg('label', $label);
126
            $args[] = new VigiloArg('ifname', $port->getName());
127
            $this->children[] = new VigiloTest('Interface', $args);
128

  
129
            // Retrieve all IP addresses associated with this interface.
130
            // This will be used later in selectAddress() to select
131
            // the most appropriate IP address to query this network.
132
            foreach ($DB->query($query2) as $nn) {
133
                $query3 = IPAddress::getSQLRequestToSearchForItem("NetworkName", $nn['id']);
134
                foreach ($DB->query($query3) as $ip) {
135
                    $addr = new IPAddress();
136
                    if ($addr->getFromDB($ip['id'])) {
137
                        $this->addresses[] = $addr;
138
                    }
139
                }
140
            }
141
        }
142
    }
143

  
144
    public function __toString()
145
    {
146
        $outXML = new DOMDocument();
147
        $outXML->preserveWhiteSpace = false;
148
        $outXML->formatOutput       = true;
149
        $outXML->loadXML(
150
            self::sprintf(
151
                '<?xml version="1.0"?>' .
152
                '<host name="%s" address="%s" ventilation="%s">%s</host>',
153
                $this->network->getName(),
154
                $this->selectAddress(),
155
                "Servers",
156
                $this->children
157
            )
158
        );
159
        return $outXML->saveXML();
160
    }
161
}
src/Vigilo/VigiloSoftwareList.php
1
<?php
2

  
3
function getSoftwareList($computer)
4
{
5
    return array(
6
        /**
7
         * Serveurs web
8
         */
9
        // Apache HTTP Server (RHEL/CentOS)
10
        'httpd'         => array("addHTTPTest", array()),
11

  
12
        // Apache HTTP Server (Debian)
13
        'apache2'       => array("addHTTPTest", array()),
14

  
15
        // AOL web server
16
        'aolserver-4'   => array("addHTTPTest", array()),
17

  
18
        // Event-based HTTP/WSGI server
19
        'gunicorn'      => array("addHTTPTest", array()),
20

  
21
        // Specialized HTTP server to access CD-ROM books
22
        'ebhttpd'       => array("addHTTPTest", array()),
23

  
24
        // Fast webserver with minimal memory footprint
25
        'lighttpd'      => array("addHTTPTest", array()),
26

  
27
        // Really small HTTP server
28
        'micro-httpd'   => array("addHTTPTest", array()),
29

  
30
        // nghttp HTTP 2.0 servers
31
        'nghttp2'       => array("addHTTPTest", array()),
32

  
33
        // Small, powerful, scalable web/proxy server
34
        'nginx'         => array("addHTTPTest", array()),
35

  
36
        // Lightweight HTTP server for static content
37
        'webfs'         => array("addHTTPTest", array()),
38

  
39
        // High performance HTTP 1.1 webserver written by Erlang
40
        'yaws'          => array("addHTTPTest", array()),
41

  
42
        // HTTP server that runs on Emacsen
43
        'elserv'        => array("addHTTPTest", array()),
44

  
45
        // Fast and very simple Ruby web server
46
        'thin'          => array("addHTTPTest", array()),
47

  
48
        // Web server providing an HTTP interface to Redis
49
        'webdis'        => array("addHTTPTest", array()),
50

  
51

  
52
        //'ntp'=> array("addNTPTest", array()), //Network Time Protocol deamon and utility programs
53

  
54

  
55
        /**
56
         * Connecteurs de Vigilo
57
         */
58
        // Vigilo module receiving and stocking metrology data
59
        'vigilo-connector-metro'        => array("addVigiloConnectorTest", array("metro")),
60

  
61
        // Vigilo module sharing data with Nagios
62
        'vigilo-connector-nagios'       => array("addVigiloConnectorTest", array("nagios")),
63

  
64
        'vigilo-connector-syncevent'    => array("addVigiloConnectorTest", array("syncevent")),
65

  
66
        'vigilo-connector-vigiconf'     => array("addVigiloConnectorTest", array("vigiconf")),
67

  
68

  
69
        /**
70
         * Nagios
71
         */
72
        // Host/service/network monitoring and management system (RHEL/CentOS)
73
        'nagios'    => array("addNagiosTest", array()),
74

  
75
        // Host/service/network monitoring and management system (Debian)
76
        'nagios3'   => array("addNagiosTest", array()),
77

  
78

  
79
        /**
80
         * Caches mémoires
81
         */
82
        // High-performance memory object caching system
83
        'memcached'  => array("addMemcachedTest", array($computer)),
84

  
85
        //Data caching daemon for RRDtool
86
        'rrdcached'  => array("addRRDcachedTest", array($computer)),
87

  
88

  
89
        /**
90
         * Serveurs SSH
91
         */
92
        // Secure shell (SSH) server, for secure access from remote machines
93
        'openssh-server'    => array("addSSHTest", array()),
94

  
95
        // Secure shell client and server (metapackage)
96
        'ssh'               => array("addSSHTest", array()),
97

  
98

  
99
        /**
100
         * Bases de données
101
         */
102
        // MariaDB database server
103
        'mariadb-server'    => array("addPGSQLTest", array($computer)),
104

  
105
        // MySQL database server
106
        'mysql-server'      => array("addPGSQLTest", array($computer)),
107

  
108

  
109
        /**
110
         * Serveurs mandataires
111
         */
112
        // Really small HTTP/HTTPS proxy
113
        'micro-proxy'   => array("addProxyTest", array($computer)),
114

  
115
        // NTLM Authorization Proxy Server
116
        'ntlmaps'       => array("addProxyTest", array($computer)),
117

  
118
        // Privacy enhancing HTTP Proxy
119
        'privoxy'       => array("addProxyTest", array($computer)),
120

  
121
        // Full featured Web Proxy cache (HTTP proxy)
122
        'squid3'        => array("addProxyTest", array($computer)),
123

  
124
        // A lightweight, non-caching, optionally anonymizing HTTP proxy
125
        'tinyproxy'     => array("addProxyTest", array($computer)),
126
    );
127
}
src/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
            }
15
            $new_args[$arg->getName()] = $arg;
16
        }
17

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

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

  
3
class VigiloTestSoftware
4
{
5
    protected $testTable;
6
    protected $softwareBase;
7
    protected $computer;
8
    protected $addedTests;
9

  
10
    public function __construct($computer)
11
    {
12
        $this->computer     = $computer;
13
        $this->softwareBase = getSoftwareList($this->computer);
14
        $this->testTable    = array();
15
        $this->addedTests   = array();
16
    }
17

  
18
    public function getTable()
19
    {
20
        return $this->testTable;
21
    }
22

  
23
    public function addRelevantTestWith($softwareName)
24
    {
25
        if (strstr($softwareName, "vigilo-test")) {
26
            $functionArray = array("addCustomTest", array($softwareName));
27
        } else {
28
            if (!array_key_exists($softwareName, $this->softwareBase)) {
29
                return;
30
            }
31
            $functionArray = $this->softwareBase[$softwareName];
32
        }
33
        $this->testTable[] = call_user_func_array(array($this, $functionArray[0]), $functionArray[1]);
34
    }
35

  
36
    protected function addCustomTest($softwareName)
37
    {
38
        $software_name = str_replace('vigilo-test-', '', $softwareName);
39
        $explode_software_name = explode('-', $software_name, 2);
40
        $args = array();
41

  
42
        switch (strtolower($explode_software_name[0])) {
43
            case "process":
44
                $args[] = new VigiloArg('processname', $explode_software_name[1]);
45
                $explode_software_name[0] = "Process";
46
                break;
47

  
48
            case "service":
49
                $args[] = new VigiloArg('svcname', $explode_software_name[1]);
50
                $explode_software_name[0] = "Service";
51
                break;
52

  
53
            case "tcp":
54
                $args[] = new VigiloArg('port', $explode_software_name[1]);
55
                $explode_software_name[0] = "TCP";
56
                break;
57

  
58
            default:
59
                return;
60
        }
61

  
62
        return new VigiloTest($explode_software_name[0], $args);
63
    }
64

  
65
    protected function addNTPTest()
66
    {
67
        $args = array();
68
        //$address=0;
69
        //$args[] = new VigiloArg('address', $address);
70
        $args[] = new VigiloArg('crit', 0);
71
        $args[] = new VigiloArg('warn', 0);
72
        return new VigiloTest('NTP', $args);
73
    }
74

  
75
    protected function addNTPqTest()
76
    {
77
        $args = array();
78
        $args[] = new VigiloArg('crit', 2000);
79
        $args[] = new VigiloArg('warn', 5000);
80
        return new VigiloTest('NTPq', $args);
81
    }
82

  
83
    protected function addNTPSyncTest() // OK
84
    {
85
        return new VigiloTest('NTPSync');
86
    }
87

  
88
    protected function addHTTPTest() // OK
89
    {
90
        return new VigiloTest('HTTP');
91
    }
92

  
93
    protected function addMemcachedTest($computer)
94
    {
95
        $args = array();
96
        $args[] = new VigiloArg('port', 11211);
97
        return new VigiloTest('Memcached', $args);
98
    }
99

  
100
    protected function addNagiosTest()
101
    {
102
        return new VigiloTest('Nagios');
103
    }
104

  
105
    protected function addPGSQLTest($computer)
106
    {
107
        $args = array();
108
        $args[] = new VigiloArg('database', "postgres");
109
        $args[] = new VigiloArg('port', 5432);
110
        $args[] = new VigiloArg('user', "postgres");
111
        return new VigiloTest('PostgreSQLConnection', $args);
112
    }
113

  
114
    protected function addProxyTest()
115
    {
116
        $args = array();
117
        $args[] = new VigiloArg('auth', "False");
118
        $args[] = new VigiloArg('port', 8080);
119
        $args[] = new VigiloArg('url', "http://www.google.fr");
120
        return new VigiloTest('Proxy', $args);
121
    }
122

  
123
    protected function addRRDcachedTest($computer)
124
    {
125
        $path = "/var/lib/vigilo/connector-metro/rrdcached.sock";
126
        $args = array();
127
        $args[] = new VigiloArg('crit', 0);
128
        $args[] = new VigiloArg('path', $path);
129
        $args[] = new VigiloArg('warn', 0);
130
        return new VigiloTest('RRDcached', $args);
131
    }
132

  
133
    protected function addSSHTest()
134
    {
135
        return new VigiloTest('SSH');
136
    }
137

  
138
    protected function addVigiloConnectorTest($type)
139
    {
140
        $args = array();
141
        $args[] = new VigiloArg('type', $type);
142
        return new VigiloTest('VigiloConnector', $args);
143
    }
144

  
145
    protected function addVigiloCorrelatorTest()
146
    {
147
        $args = array();
148
        //$args[] = new VigiloArg('rules', '');
149
        $args[] = new VigiloArg('servicename', 'vigilo-correlator');
150
        return new VigiloTest('VigiloCorrelator', $args);
151
    }
152

  
153
    protected function addTestService($computer, $service)
154
    {
155
        $args = array();
156
        $args[] = new VigiloArg('svcname', $service);
157
        return new VigiloTest('Service', $args);
158
    }
159

  
160
    public function __toString()
161
    {
162
        return $this->child;
163
    }
164
}
src/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
            } elseif (is_array($arg)) {
17
                $acc = '';
18
                foreach ($arg as $sub) {
19
                    if (is_object($sub)) {
20
                        $acc .= (string) $sub;
21
                    }
22
                }
23
                $new_args[] = $acc;
24
            } else {
25
                $new_args[] = (string) $arg;
26
            }
27
        }
28

  
29
        return vsprintf($s, $new_args);
30
    }
31
}
src/ajax/getVTValue.php
1
<?php
2

  
3
if (strpos(filter_input(INPUT_SERVER, "PHP_SELF"), "getVTValue.php")) {
4
    include(dirname(dirname(__DIR__)) .
5
            DIRECTORY_SEPARATOR . "inc" .
6
            DIRECTORY_SEPARATOR . "includes.php");
7

  
8
    header("Content-Type: text/html; charset=UTF-8");
9
    Html::header_nocache();
10
}
11

  
12
if (!defined('GLPI_ROOT')) {
13
    die("Can not acces directly to this file");
14
}
15

  
16
$ret = array();
17
$tmp = PluginVigiloVigiloTemplate::getAjaxArrayTemplates();
18

  
19
foreach ($tmp as $template) {
20
    $ret['results'][] = $template;
21
}
22

  
23
$ret['count'] = count($ret['results']);
24

  
25
echo json_encode($ret);
src/autoloader.php
1
<?php
2

  
3
function vigilo_autoloader($class_name)
4
{
5
    if (!strncmp($class_name, 'Vigilo', 6)) {
6
        require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Vigilo' . DIRECTORY_SEPARATOR . $class_name . '.php');
7
    }
8
}
src/front/menu.php
1
<?php
2

  
3
include(dirname(dirname(__DIR__)) .
4
        DIRECTORY_SEPARATOR . "inc" .
5
        DIRECTORY_SEPARATOR . "includes.php");
6

  
7
if (PluginVigiloMenu::canView()) {
8
    Html::header(
9
        __('Vigilo', 'vigilo'),
10
        $_SERVER["PHP_SELF"],
11
        "plugins",
12
        "PluginVigiloMenu",
13
        "menu"
14
    );
15

  
16
    $res = null;
17
    $pipes = array();
18

  
19
    if (!empty($_POST["deploy"])) {
20
        $fds = array(
21
            1 => array("pipe", "w"),
22
            2 => array("pipe", "w"),
23
        );
24
        $cmd = "/usr/bin/sudo -n /usr/bin/vigiconf deploy -f --debug";
25
        $res = proc_open($cmd, $fds, $pipes);
26
        if (!is_resource($res)) {
27
            $res = false;
28
        }
29
    }
30
    PluginVigiloMenu::displayMenu($res, $pipes);
31
} else {
32
    Html::displayRightError();
33
}
34

  
35
Html::footer();
src/hook.php
1
<?php
2

  
3
require(__DIR__ . DIRECTORY_SEPARATOR . 'autoloader.php');
4
require(__DIR__ . DIRECTORY_SEPARATOR . 'Vigilo' . DIRECTORY_SEPARATOR . 'VigiloSoftwareList.php');
5
require(__DIR__ . DIRECTORY_SEPARATOR . 'vigilo_hooks.php');
6

  
7
$confdir = getenv('VIGILO_CONFDIR', true);
8
if (!$confdir || !file_exists($confdir)) {
9
    $confdir = implode(DIRECTORY_SEPARATOR, array('etc', 'vigilo', 'vigiconf', 'conf.d'));
10
}
11
define('VIGILO_CONFDIR', $confdir);
12
spl_autoload_register('vigilo_autoloader');
src/inc/computer.class.php
1
<?php
2

  
3
class PluginVigiloComputer extends Computer
4
{
5
    public static function showComputerInfo($item)
6
    {
7
        global $CFG_GLPI;
8
        $templates = PluginVigiloVigiloTemplate::getAllTemplates();
9
        $value = $item->getField('vigilo_template');
10
        if ($value === 'NULL') {
11
            $value = '-----';
12
        }
13
        echo '<table class="tab_cadre_fixe tab_glpi" width="100%">';
14
        echo '<tr class="tab_bg_1"><th colspan="4">Vigilo Template</th></tr>';
15
        echo '<tr class="tab_bg_1">';
16
        echo '<td>Vigilo Template</td>';
17
        echo '<td>';
18
        Dropdown::show(
19
            'PluginVigiloComputer',
20
            array(
21
                "name" => "vigilo_template",
22
                "emptylabel" => $value,
23
                "url" => $CFG_GLPI["root_doc"] . "/plugins/vigilo/ajax/getVTValue.php"
24
            )
25
        );
26
        $ret = array();
27
        $tmp = PluginVigiloVigiloTemplate::getAllTemplates();
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff