Project

General

Profile

Revision 18eae138

ID18eae1387634d13a812564306fe66669d4aa49b6
Parent 8b28e91d
Child c482ac76

Added by Romain Chollet about 7 years ago

[#1575] Imprimantes déclarées dans glpi suivies dans vigilo

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

View differences:

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 && $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 (!($location->getName()=='N/A')) {
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 (!($entity->getName()=='N/A')) {
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 (!($manufacturer->getName()=='N/A')) {
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  = isset($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
}
hook.php
11 11
        $this->confdir = $confdir;
12 12
    }
13 13

  
14
    public function updateGroups()
14
    public function saveHost($host, $dir_type)
15 15
    {
16
        $host       = new VigiloLocation();
17
        $dirs       = array($this->confdir, "groups", "managed");
16
        $dirs       = array($this->confdir, $dir_type, "managed");
18 17
        $confdir    = implode(DIRECTORY_SEPARATOR, $dirs);
19
        $file       = $confdir . DIRECTORY_SEPARATOR . "groups.xml";
18
        $file       = $confdir . DIRECTORY_SEPARATOR . $host->getName() . ".xml";
20 19

  
21
        mkdir($confdir, 0770, true);
22
        $acc = "";
23
        foreach ($dirs as $dir) {
24
            $acc .= DIRECTORY_SEPARATOR . $dir;
25
            chgrp($acc, "vigiconf");
20
        if (!file_exists($confdir)) {
21
            mkdir($confdir, 0770, true);
26 22
        }
27 23

  
28 24
        $res = file_put_contents($file, $host, LOCK_EX);
......
32 28
        }
33 29
    }
34 30

  
31
    public function updateGroups()
32
    {
33
        $host       = new VigiloLocation();
34
        $this->saveHost($host, "groups");
35
    }
36

  
35 37
    public function addComputer($computer)
36 38
    {
37 39
        if ($computer->getField("is_template")==0) {
......
49 51
                      SET is_dynamic = ' 1
50 52
                      ' WHERE id = " . $computer->getField("id") . ";";
51 53
            $DB->queryOrDie($query, "update vigilo_template field");
52
            $host       = new VigiloHost($computer);
53
            $dirs       = array($this->confdir, "hosts", "managed");
54
            $confdir    = implode(DIRECTORY_SEPARATOR, $dirs);
55
            $file     = $confdir . DIRECTORY_SEPARATOR . $host->getName() . ".xml";
56

  
57
            if (!file_exists($confdir)) {
58
                mkdir($confdir, 0770, true);
59
            }
60 54

  
61
            $res = file_put_contents($file, $host, LOCK_EX);
62
            if ($res !== false) {
63
                chgrp($file, "vigiconf");
64
                chmod($file, 0660);
65
            }
55
            $host = new VigiloHost($computer);
56
            $this->saveHost($host, "hosts");
66 57
        }
67 58
    }
68 59

  
......
71 62
        if ($networkequipment->getField("is_template")==0) {
72 63
            global $DB;
73 64

  
74
            $host       = new VigiloNetworkEquipment($networkequipment);
75
            $dirs       = array($this->confdir, "hosts", "managed");
76
            $confdir    = implode(DIRECTORY_SEPARATOR, $dirs);
77
            $file       = $confdir . DIRECTORY_SEPARATOR . $host->getName() . ".xml";
65
            $host = new VigiloNetworkEquipment($networkequipment);
66
            $this->saveHost($host, "hosts");
67
        }
68
    }
78 69

  
79
            if (!file_exists($confdir)) {
80
                mkdir($confdir, 0770, true);
81
            }
70
    public function addPrinter($printer)
71
    {
72
        if ($printer->getField("is_template")==0) {
73
            global $DB;
82 74

  
83
            $res = file_put_contents($file, $host, LOCK_EX);
84
            if ($res !== false) {
85
                chgrp($file, "vigiconf");
86
                chmod($file, 0660);
87
            }
75
            $host = new VigiloPrinter($printer);
76
            $this->saveHost($host, "hosts");
88 77
        }
89 78
    }
90 79

  
......
113 102
        $this->addNetworkEquipment($networkEquipment);
114 103
    }
115 104

  
105
    public function updatePrinter($printer)
106
    {
107
        $this->update($printer);
108
        $this->addPrinter($printer);
109
    }
110

  
116 111
    public function unmonitor($host)
117 112
    {
118 113
        $dirs = array($this->confdir, "hosts", "managed", $host . ".xml");
119
        unlink(implode(DIRECTORY_SEPARATOR, $dirs));
114
        $filename = implode(DIRECTORY_SEPARATOR, $dirs);
115
        if (file_exists($filename))
116
        {
117
            unlink($filename);
118
        }
120 119
    }
121 120

  
122 121
    public function manageComputerSoftwareVersion($computer_software_version)
......
181 180
            $ne->getFromDB($id);
182 181
            $this->updateNetworkEquipment($ne);
183 182
        }
183
        else if ($itemtype === 'Printer') {
184
            $printer=new Printer();
185
            $printer->getFromDB($id);
186
            $this->updatePrinter($printer);
187
        }
184 188
    }
185 189

  
186 190
    public function plugin_vigilo_getAddSearchOptions($itemtype)
setup.php
12 12
    $hooks['csrf_compliant'][$p]        = true;
13 13
    $hooks['item_add'][$p]              = array("Computer" => array($hookObj, "addComputer"),
14 14
                                                "NetworkEquipment" => array($hookObj, "addNetworkEquipment"),
15
                                                "Printer" => array($hookObj, "addPrinter"),
15 16
                                                "ComputerDisk" => array($hookObj,"manageDisks"),
16 17
                                                "NetworkPort" => array($hookObj,"manageNetworks"),
17 18
                                                "IPAddress" => array($hookObj,"manageAddresses"),
......
27 28
                                                "Manufacturer" => array($hookObj,"updateGroups"));
28 29
    $hooks['item_update'][$p]           = array("Computer" => array($hookObj, "updateComputer"),
29 30
                                                "NetworkEquipment" => array($hookObj, "updateNetworkEquipment"),
31
                                                "Printer" => array($hookObj, "updatePrinter"),
30 32
                                                "ComputerDisk" => array($hookObj,"manageDisks"),
31 33
                                                "NetworkPort" => array($hookObj,"manageNetworks"),
32 34
                                                "IPAddress" => array($hookObj,"manageAddresses"),
......
42 44
                                                "Manufacturer" => array($hookObj,"updateGroups"));
43 45
    $hooks['item_purge'][$p]            = array("Computer" => array($hookObj, "delete"),
44 46
                                                "NetworkEquipment" => array($hookObj, "delete"),
47
                                                "Printer" => array($hookObj, "delete"),
45 48
                                                "ComputerDisk" => array($hookObj,"manageDisks"),
46 49
                                                "NetworkPort" => array($hookObj,"manageNetworks"),
47 50
                                                "IPAddress" => array($hookObj,"manageAddresses"),
......
57 60
                                                "Manufacturer" => array($hookObj,"updateGroups"));
58 61
    $hooks['item_delete'][$p]           = array("Computer" => array($hookObj, "delete"),
59 62
                                                "NetworkEquipment" => array($hookObj, "delete"),
63
                                                "Printer" => array($hookObj, "delete"),
60 64
                                                "ComputerDisk" => array($hookObj,"manageDisks"),
61 65
                                                "NetworkPort" => array($hookObj,"manageNetworks"),
62 66
                                                "IPAddress" => array($hookObj,"manageAddresses"),
......
72 76
                                                "Manufacturer" => array($hookObj,"updateGroups"));
73 77
    $hooks['item_restore'][$p]          = array("Computer" => array($hookObj, "addComputer"),
74 78
                                                "NetworkEquipment" => array($hookObj, "addNetworkEquipment"),
79
                                                "Printer" => array($hookObj, "addPrinter"),
75 80
                                                "ComputerDisk" => array($hookObj,"manageDisks"),
76 81
                                                "NetworkPort" => array($hookObj,"manageNetworks"),
77 82
                                                "IPAddress" => array($hookObj,"manageAddresses"),

Also available in: Unified diff