Skip to content

Commit 558ecce

Browse files
author
Debug Bill
committed
ODBC is OK, updates to MDT when anything change on PC is OK too
1 parent f76efca commit 558ecce

File tree

10 files changed

+169
-175
lines changed

10 files changed

+169
-175
lines changed

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ It is developed and tested in this environment only. If you experience problem w
3030

3131
* MDT must be installed in a MS-SQL database accessible from the GLPI server
3232
* MDT must be fully operationnal by itself. The plugin will not fix a faulty MDT installation, it is only remote-controlling it.
33-
* MSSQL and SimpleXML PHP modules must be installed
33+
* (MSSQL or ODBC) and SimpleXML PHP modules must be installed
3434
* The "Control" directory in your MDT deployment share contains part of the MDT configuration (the other part is in the MS-SQL database). It needs to be mounted (read-only is OK) somewhere on your GLPI server and accessible to PHP scripts.
3535
* Fusion inventory, though not mandatory is really very very nice to have.
3636

@@ -42,14 +42,11 @@ The plugin is available in French and English, other translators welcome.
4242

4343

4444
## TODOs
45-
* Add rights management. Currently anyone can do anything
46-
* Make it possible to choose more than one application. Manage groups (this is mainly UI interface stuff, the code can already handle it. My main question is how many applications are avalaible on average MDT servers? Our has only 5, but in a previous company we had hundreds. The gui would not be the same....)
45+
* Manage groups (this is mainly UI interface stuff, the code can already handle it. My main question is how many applications are avalaible on average MDT servers? Our has only 5, but in a previous company we had hundreds. The gui would not be the same....)
4746
* Handle roles, models, packages in the same way applications are handled (but are you using those features in MDT)?
4847
* Automate some actions based on information available in GLPI and not managed by MDT (location, entity....)
4948
* Be multi-MDT-server, multi-deployment-share, multi-domain aware. Currently the plugin is not domain aware and is connected to only one MDT database. This raises quite a few questions as to how it should work then.
5049
* Several coupling modes are proposed in the config page.... but only one is really implemented. There too I wonder what user want. For me the "Strict Master-Slave is fine" as I want everything to be done in GLPI.
51-
* Add "database port" support. This is easy but as I did not need it it was low on the list
52-
* Add a few crontasks to automate data import from MDT and data cleanup, especially in Master-Master mode if I ever implement it.
5350
* Fix Bugs!!!!! There most probably are many, this is the first release.
5451

5552

hook.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,14 @@ function plugin_glpi2mdt_uninstall() {
265265

266266
/**
267267
* This function is called by GLPI when an update is made to a computer
268+
* It triggers an update of MDT just in case...
268269
*
269-
* @param GLPI object ID, here a computer
270-
* @param Expire: will only reset "OSInstall flag set to true and coupling mode is not "strict master slave"
271-
* @return string type for the cron list page
270+
* @param $item, object reference to a computer
271+
* @return nothing
272272
*/
273-
function update_item(computer $item) {
274-
Session::addMessageAfterRedirect("Glpi2mdt after update hook", true);
275-
print_f($item);
276-
die("Here we are");
273+
function updateMDT($item) {
274+
$id = $item->getID();
275+
$computer = new PluginGlpi2mdtComputer;
276+
$computer->updateMDT($id);
277+
unset($computer);
277278
}

inc/computer.class.php

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,25 @@ class PluginGlpi2mdtComputer extends PluginGlpi2mdtMdt {
4949
* inside the left menu of a Itemtype.
5050
*/
5151
function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
52-
return self::createTabEntry(__('Auto Install', 'glpi2mdt'));
52+
global $DB;
53+
$id = $item->getID();
54+
$result = $DB->query("SELECT value FROM glpi_plugin_glpi2mdt_settings
55+
WHERE type='C' AND category='C' AND `key`='OSInstall' AND id=$id");
56+
$row = $DB->fetch_array($result);
57+
if ($row['value'] == "YES") {
58+
return self::createTabEntry(__('Auto Install', 'glpi2mdt'), __('YES'));
59+
} else {
60+
return self::createTabEntry(__('Auto Install', 'glpi2mdt'), __('NO'));
61+
}
5362
}
5463

64+
5565
/**
56-
* Update GLPI database
66+
* This function is called by the computer form for glpi2mdt when pressing "save"
67+
* It parses the post variables and stores them in the GLPI database
68+
*
69+
* @param $post, the full list of post items
70+
* @return nothing
5771
*/
5872
function updateValue($post) {
5973
global $DB;
@@ -63,7 +77,7 @@ function updateValue($post) {
6377
return false;
6478
}
6579

66-
// Build array of valid parameters
80+
// Build array of valid parameters for post variables
6781
$result = $DB->query("SELECT column_name FROM glpi_plugin_glpi2mdt_descriptions WHERE is_deleted=false");
6882
while ($line = $DB->fetch_array($result)) {
6983
$parameters[$line['column_name']] = true;
@@ -72,14 +86,12 @@ function updateValue($post) {
7286
if (isset($post['id']) and ($post['id'] > 0)) {
7387
$id = $post['id'];
7488
$apprank=0;
75-
// Mark lines to be deleted
76-
$DB->queryOrDie("UPDATE glpi_dev.glpi_plugin_glpi2mdt_settings SET value=0
77-
WHERE type='C' AND category='A' AND id=$id");
89+
// Delete all computer configuration entries
7890
$DB->queryOrDie("DELETE FROM glpi_plugin_glpi2mdt_settings WHERE id=$id and type='C'");
7991
foreach ($post as $key=>$value) {
8092
// only valid parameters may be inserted. The full list is in the "descriptions" database
8193
if (isset($parameters[$key])) {
82-
$query = "INSERT INTO glpi_dev.glpi_plugin_glpi2mdt_settings
94+
$query = "INSERT INTO glpi_plugin_glpi2mdt_settings
8395
(`id`, `category`, `type`, `key`, `value`)
8496
VALUES ($id, 'C','C', '$key', '$value')
8597
ON DUPLICATE KEY UPDATE value='$value'";
@@ -89,46 +101,42 @@ function updateValue($post) {
89101
if ((substr($key, 0, 4) == 'App-') and ($value <> 'none') and (strlen($key) == 42) and ($value == 'on')) {
90102
$guid = substr($key, 4, strlen($key)-4);
91103
$apprank += 1;
92-
$query = "INSERT INTO glpi_dev.glpi_plugin_glpi2mdt_settings
104+
$query = "INSERT INTO glpi_plugin_glpi2mdt_settings
93105
(`id`, `category`, `type`, `key`, `value`)
94106
VALUES ($id, 'A','C', '$guid', '$apprank')
95107
ON DUPLICATE KEY UPDATE value='$apprank'";
96-
$DB->queryOrDie($query, "Cannot update settings database");
108+
$DB->queryOrDie($query, "Cannot update application settings database");
97109
}
98110
// Roles
99111
if ((substr($key, 0, 6) == 'Roles-') and ($value <> 'none') and ($value == 'on')) {
100112
$guid = substr($key, 6, strlen($key)-6);
101113
$rolerank += 1;
102-
$query = "INSERT INTO glpi_dev.glpi_plugin_glpi2mdt_settings
114+
$query = "INSERT INTO glpi_plugin_glpi2mdt_settings
103115
(`id`, `category`, `type`, `key`, `value`)
104116
VALUES ($id, 'R','C', '$guid', '$rolerank')
105117
ON DUPLICATE KEY UPDATE value='$rolerank'";
106-
$DB->queryOrDie($query, "Cannot update settings database");
118+
$DB->queryOrDie($query, "Cannot update roles settings database");
107119
}
108120
if (($key == 'OSInstallExpire')) {
109121
$timestamp = strtotime($value);
110122
if ($timestamp > 0) {
111-
$query = "INSERT INTO glpi_dev.glpi_plugin_glpi2mdt_settings
123+
$query = "INSERT INTO glpi_plugin_glpi2mdt_settings
112124
(`id`, `category`, `type`, `key`, `value`)
113125
VALUES ($id, 'C','C', '$key', '$timestamp')
114126
ON DUPLICATE KEY UPDATE value='$timestamp'";
115-
} else {
116-
$query = "DELETE FROM glpi_dev.glpi_plugin_glpi2mdt_settings WHERE id=$id AND category='C' AND type='C' AND key='$key';";
117127
}
118128
$DB->query($query);
119129
}
120130
}
121-
$DB->queryOrDie("DELETE FROM glpi_dev.glpi_plugin_glpi2mdt_settings
122-
WHERE type='C' AND category='A' AND id=$id AND value=0");
123131
}
124132
}
125133

126134
/**
127135
* Updates the MDT MSSQL database with information contained in GLPI's database
128136
*
129137
* @param GLPI object ID, here a computer
130-
* @param Expire: will only reset "OSInstall flag set to true and coupling mode is not "strict master slave"
131-
* @return string type for the cron list page
138+
* @param Expire: will only reset "OSInstall" flag set to true and coupling mode is not "strict master slave"
139+
* @return nothing
132140
*/
133141
function updateMDT($id) {
134142
global $DB;
@@ -151,7 +159,7 @@ function updateMDT($id) {
151159
$common = $DB->fetch_array($result);
152160

153161
// Build list of IDs of existing records in MDT bearing same name, uuid, serial or mac adresses
154-
// as the computer being updated (this might clean up other bogus entries and remove duplicate names
162+
// as the computer being updated (this might clean up other bogus entries and remove duplicate names
155163
$uuid = $common['uuid'];
156164
$name = $common['name'];
157165
$serial = $common['serial'];
@@ -165,10 +173,16 @@ function updateMDT($id) {
165173
AND c.is_deleted=FALSE AND n.is_deleted=false");
166174
$macs="MacAddress IN (";
167175
unset($values);
176+
$nblines = 0;
168177
while ($line = $DB->fetch_array($result)) {
169178
$mac = $line['mac'];
170179
$macs=$macs."'".$mac."', ";
171180
$values = $values."('$name', '$uuid', '$serial', '$assettag', '$mac'), ";
181+
$nblines +=1;
182+
}
183+
// There should be one line per mac address in MDT, and at least one if no mac is provided.
184+
if ($nblines == 0) {
185+
$nblines = 1;
172186
}
173187
$macs = substr($macs, 0, -2).") ";
174188
$values = substr($values, 0, -2)." ";
@@ -184,7 +198,8 @@ function updateMDT($id) {
184198
OR $macs";
185199
$result = $this->queryOrDie("$query", "Can't read IDs to delete");
186200

187-
// build a list of IDs
201+
// build a list of MDT IDs corresponding to the computer in GLPI (there can be several
202+
// because of multiple mac addresses or duplicate names, serials....
188203
$ids = "ID IN (";
189204
while ($line = $this->fetch_array($result)) {
190205
$ids=$ids."'".$line['ID']."', ";
@@ -193,27 +208,33 @@ function updateMDT($id) {
193208
if ($ids == "ID IN)") {
194209
$ids="ID = ''";
195210
}
196-
$query = "DELETE FROM dbo.ComputerIdentity WHERE $ids";
197-
$this->queryOrDie($query);
198-
199-
// Delete corresponding records in side tables
200-
$this->queryOrDie("DELETE FROM dbo.Settings WHERE Type='C' and $ids");
201-
$this->queryOrDie("DELETE FROM dbo.Settings_Applications WHERE Type='C' and $ids");
202-
$this->queryOrDie("DELETE FROM dbo.Settings_Administrators WHERE Type='C' and $ids");
203-
$this->queryOrDie("DELETE FROM dbo.Settings_Packages WHERE Type='C' and $ids");
204-
$this->queryOrDie("DELETE FROM dbo.Settings_Roles WHERE Type='C' and $ids");
205-
206-
$query = "INSERT INTO dbo.ComputerIdentity (Description, UUID, SerialNumber, AssetTag, MacAddress) VALUES $values";
207-
$this->queryOrDie("$query");
208-
209-
// Retreive newly created entries ids in order to add the settings.
211+
// Check if the computer entries in MDT are the ones expected by GLPI.
212+
// If no, delete everything and recreate
213+
// If yes, depending on coupling mode, delete and recreate or simply update
214+
$query = ("SELECT FROM dbo.ComputerIdentity
215+
WHERE (UUID='$uuid') AND Description='$name' AND SerialNumber='$serial' AND $macs");
216+
$result = $this->query($query);
217+
$nblinesactual = $this->numrows($result);
218+
if ($nblines <> $nblinesactual) {
219+
$this->queryOrDie("DELETE FROM dbo.ComputerIdentity WHERE $ids");
220+
$this->queryOrDie("INSERT INTO dbo.ComputerIdentity (Description, UUID, SerialNumber, AssetTag, MacAddress) VALUES $values");
221+
}
222+
// Delete corresponding records in side tables depending on coupling mode
223+
if ($nblines <> $nblinesactual OR $mode == 'strict') {
224+
$this->queryOrDie("DELETE FROM dbo.Settings WHERE Type='C' and $ids");
225+
$this->queryOrDie("DELETE FROM dbo.Settings_Applications WHERE Type='C' and $ids");
226+
$this->queryOrDie("DELETE FROM dbo.Settings_Administrators WHERE Type='C' and $ids");
227+
$this->queryOrDie("DELETE FROM dbo.Settings_Packages WHERE Type='C' and $ids");
228+
$this->queryOrDie("DELETE FROM dbo.Settings_Roles WHERE Type='C' and $ids");
229+
}
230+
// Retreive (newly created or not) entries ids in order to add the settings.
210231
//Name is enough as we cleaned bogus entries in the previous phase
211232
$query = "SELECT ID FROM dbo.ComputerIdentity WHERE Description='$name'";
212233
$result = $this->queryOrDie($query);
213234
$ids = "(";
214235
$values = '';
215236

216-
while ($row = mssql_fetch_array($result)) {
237+
while ($row = $this->fetch_array($result)) {
217238
$mdtid = $row['ID'];
218239
$ids = $ids.$mdtid.", ";
219240
$values = $values."('C', $mdtid, '$name', '$name', '$name', '$adminpasscomposite'), ";
@@ -265,7 +286,7 @@ function updateMDT($id) {
265286
}
266287

267288
/**
268-
* This function is called from GLPI to render the form when the user click
289+
* This function is called from GLPI to render the form when the user clicks
269290
* on the menu item generated from getTabNameForItem()
270291
*/
271292
static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
@@ -275,7 +296,7 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtempl
275296
$osinstall = 'NO';
276297
$osinstallexpire = date('Y-m-d H:i', 300*ceil(time()/300) + (3600*24));
277298

278-
$query = "SELECT `category`, `key`, `value` FROM glpi_dev.glpi_plugin_glpi2mdt_settings WHERE type='C' AND id='$id'";
299+
$query = "SELECT `category`, `key`, `value` FROM glpi_plugin_glpi2mdt_settings WHERE type='C' AND id='$id'";
279300
$result = $DB->query($query);
280301
while ($row=$DB->fetch_array($result)) {
281302
if ($row['category'] == 'C') {

inc/mdt.class.php

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,12 @@ function __construct() {
9292
$this->DBModule = 'odbc';
9393
$DBLink = odbc_connect("Driver=$DBDriver;Server=$DBServer,$DBPort;Database=$DBSchema;", $DBLogin, $DBPassword);
9494

95-
// Conection to MSSQL using native MSSQL PHP module available until PHP 5.6
95+
// Conection to MSSQL using native MSSQL PHP module available until PHP 5.6
9696
} else if (extension_loaded('mssql')) {
9797
$DBModule = 'mssql';
9898
$DBLink = mssql_connect($DBServer.":".$DBPort, $DBLogin, $DBPassword);
9999
if ($DBLink) {
100-
if (mssql_select_db($DBSchema, $MDT)) {
101-
}
100+
mssql_select_db($DBSchema, $MDT);
102101
}
103102
}
104103
$this->DBLink = $DBLink;
@@ -132,31 +131,31 @@ function __destruct() {
132131
**/
133132
function showTestConnection() {
134133
$dbschema = $this->globalconfig['DBSchema'];
134+
135135
echo '<table class="tab_cadre_fixe">';
136136
echo '<tr class="tab_bg_1">';
137-
echo '<td>';
137+
echo '<th>'.__("Testing connection using PHP module", 'glpi2mdt')." ".$this->DBModule.'</th></tr><tr><td>';
138138
// Connection to MSSQL
139139
if ($this->DBLink) {
140-
echo "<h1><font color='green'>";
141-
echo _e("Database login OK!", 'glpi2mdt');
142-
echo "</font></h1><br>";
140+
echo "<font color='green'>";
141+
echo __("Database login OK!", 'glpi2mdt');
142+
echo "</font><br>";
143143
// Simple query to get database version
144144
$version = $this->query('SELECT @@VERSION');
145145
$row = $this->fetch_array($version);
146146
echo "Server is: <br>".reset($row)."<br>";
147-
$result = $this->query("SELECT COUNT(*) FROM $dbschema.information_schema.tables WHERE table_type='base table'");
147+
$result = $this->query("SELECT COUNT(*) FROM $dbschema.information_schema.tables WHERE table_type='base table'");
148148
$nb = reset($this->fetch_array($result));
149149
if ($nb > 0) {
150-
echo "<h1><font color='green'>";
151-
echo __(" Schema ", 'glpi2mdt').$dbschema.__(" contains ",'glpi2mdt').$nb.__(" tables ",'glpi2mdt');
152-
echo "</font></h1><br>";
150+
echo "<font color='green'>";
151+
echo __(" Schema ", 'glpi2mdt').$dbschema.__(" contains ", 'glpi2mdt').$nb.__(" tables ", 'glpi2mdt');
152+
echo "</font><br>";
153153
} else {
154154
echo "<h1><font color='red'>";
155-
echo __("Could not count tables in schema ",'glpi2mdt').$this->DBSchema;
155+
echo __("Could not count tables in schema ", 'glpi2mdt').$this->DBSchema;
156156
echo "</font></h1><br>";
157157
}
158-
}
159-
else {
158+
} else {
160159
echo "<h1><font color='red'>";
161160
echo __("Database login KO!", 'glpi2mdt');
162161
echo "</font></h1><br>";
@@ -166,31 +165,6 @@ function showTestConnection() {
166165
}
167166

168167

169-
function showTestConnection2() {
170-
171-
echo '<table class="tab_cadre_fixe" width="100%"><tr class="headerRow">';
172-
echo '<th colspan="2">'.__("Connection tested using PHP module",'glpi2mdt')." ".$this->DBModule.'</th>';
173-
echo '</tr><tr class="tab_bg_1><td>';
174-
// Connection to MSSQL
175-
if ($this->DBLink) {
176-
echo "<h1><font color='green'>";
177-
echo _e("Database login OK!", 'glpi2mdt');
178-
echo "</font></h1><br>";
179-
// Simple query to get database version
180-
$version = $this->queryOrDie('SELECT @@VERSION', "does not work");
181-
$row = $this->fetch_array($version);
182-
echo "Server is: <br>".print_r($row)."<br>";
183-
}
184-
else {
185-
echo "<h1><font color='red'>";
186-
echo _e("Database login KO!", 'glpi2mdt');
187-
echo "</font></h1><br>";
188-
}
189-
echo '</td>';
190-
echo '</tr></table>';
191-
}
192-
193-
194168
/**
195169
* Several functions to interact with MDT database
196170
* Uses PHP module odbc or mssql as appropriate

locales/en_US.mo

1.13 KB
Binary file not shown.

0 commit comments

Comments
 (0)