r4764 updated revision by Neil Scholten
[racktables-contribs] / facter.php
CommitLineData
38ba29a5
AD
1<?php
2/*
3 * Facter (Puppet) plugin for racktables 0.19.1 (and probably newer)
1806a4df
DO
4*
5* This file is both a web GUI and REST webservice for auto create and update machines based on facter (puppet) files.
6*
7* REST examples:
8* curl -k -F myfile=@facter.txt -u username:password https://facter/racktables/process.php?page=depot&tab=facter&op=Update
9* curl -F myfile=@facter.txt -u username:password http://facter/racktables/process.php?page=depot&tab=facter&op=Update
10*
11* Usage instructions:
12* * Just symlink it to your inc folder
13* * add it to the local.php file inn your inc folder: echo "<?php include_once 'facter.php'; ?>" >> inc/local.php
14* * If you want you can add the CPU attribute and it will show you your cpu's aswell
15*
16* Author: Torstein Hansen <huleboer@users.sourceforge.net>, sponsored by eniro.no
17*
18* This script is based on yaml_import for racktables by Tommy Botten Jensen
19*
20* 2011-08-25 modified by Neil Scholten <neil.scholten@gamigo.com>
21* - adjusted path for racktables > 0.19.1
22* - modified .yaml parsing to match to 'facter -py' format
23* - modified interface-type detection to use virtual port on VMs
24* - modified OS detection to match more better default sets (Testcase: CentOS).
25*
26*/
38ba29a5
AD
27
28
29// Depot Tab for objects.
30$tab['depot']['facter'] = 'Facter';
31$tabhandler['depot']['facter'] = 'ViewHTML';
32$ophandler['depot']['facter']['Update'] = 'Update';
33
34
35// The ophandler to insert objects (if any)
36function Update()
37{
1806a4df
DO
38 // Read uploaded file
39 $lines = file($_FILES['userfile']['tmp_name']);
40
41 // add file contents to facter array
42 foreach ($lines as $line_num => $line)
43 {
44 $tmpfacter=explode(":",$line,2);
45 $facter[trim($tmpfacter[0])]=str_replace('"', '',trim($tmpfacter[1]));
46 }
47
48 // Fix fqdn since all fields have \n inn them
49 $facter['fqdn']=str_replace("\n","", $facter['fqdn']);
50
51 // Check if it's an existing machine
52 $query = "select id from RackObject where name = \"$facter[fqdn]\" LIMIT 1";
53 unset($result);
54 $result = usePreparedSelectBlade ($query);
55 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
56 if($resultarray) {
57 $id=$resultarray[0]['id'];
58 }
59
60 // If it's a new machine
61 if (! isset($id))
62 {
63 // Check to see if it's a physical machine and get the correct id for Server
64 if ($facter['is_virtual']=="false\n")
65 {
66 // Find server id
67 $query = "select dict_key from Dictionary where dict_value='Server' LIMIT 1";
68 unset($result);
69 $result = usePreparedSelectBlade ($query);
70 $resultarray = $result->fetchAll ();
71 if($resultarray)
72 {
73 $virtual=$resultarray[0]['dict_key'];
74 }
75 }
76 // Check to see if it's a virtual machine and get the correct id for VM
77 else
78 {
79 // Find virtual id
80 $query = "select dict_key from Dictionary where dict_value='VM' LIMIT 1";
81 unset($result);
82 $result = usePreparedSelectBlade ($query);
83 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
84 if($resultarray)
85 {
86 $virtual=$resultarray[0]['dict_key'];
87 }
88 }
89 // Add the new machine
90 $newmachine=commitAddObject($facter['fqdn'],$facter['fqdn'],$virtual,$value = "");
91 }
92 // If it's an existing machine
93 else
94 {
95 // Just set some fields I use later down for updating
96 $newmachine=$id;
97 $machineupdate=1;
98 }
99
100
101
102 // Add lot's of attributes to the machine. Next version use an array (from config file) and do a loop for most of these fields.....
103
104 // Find FQDN id
105 $query = "select id from Attribute where name='FQDN' LIMIT 1";
106 unset($result);
107 $result = usePreparedSelectBlade ($query);
108 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
109 if($resultarray) {
110 $id=$resultarray[0]['id'];
111 // Update FQDN
112 commitUpdateAttrValue ($object_id = $newmachine, $attr_id = $id, $value = $facter['fqdn']);
113 }
114
115
116 // Find HW type id
117 $query = "select id from Attribute where name='HW type' LIMIT 1";
118 unset($result);
119 $result = usePreparedSelectBlade ($query);
120 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
121 if($resultarray) {
122 $id=$resultarray[0]['id'];
123 // Update HW type
124 $hw_dict_key = getdict($hw=$facter['productname'], $chapter=11 );
125 commitUpdateAttrValue ($object_id = $newmachine, $attr_id = $id, $value = $hw_dict_key);
126 }
127
128
129 // Find SW type id (OS)
130 $query = "select id from Attribute where name='SW type' LIMIT 1";
131 unset($result);
132 $result = usePreparedSelectBlade ($query);
133 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
134 if($resultarray) {
135 $id=$resultarray[0]['id'];
136 // Update SW type (OS)
137 $osrelease = $facter['lsbdistid'] . '%GSKIP%' . $facter['lsbdistid'] . ' V' . $facter['lsbmajdistrelease'];
138 $os_dict_key = getdict($hw=$osrelease, $chapter=13);
139 commitUpdateAttrValue ($object_id = $newmachine, $attr_id = $id, $value = $os_dict_key);
140 }
141
142
143 // Find OEM S/N 1
144 $query = "select id from Attribute where name='OEM S/N 1' LIMIT 1";
145 unset($result);
146 $result = usePreparedSelectBlade ($query);
147 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
148 if($resultarray) {
149 $id=$resultarray[0]['id'];
150 // Update serial number
151 commitUpdateAttrValue ($object_id = $newmachine, $attr_id = $id, $value = $facter['serialnumber']);
152 }
153
154
155 // Find Architecture id
156 $query = "select id from Attribute where name='Architecture' LIMIT 1";
157 unset($result);
158 $result = usePreparedSelectBlade ($query);
159 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
160 if($resultarray) {
161 $id=$resultarray[0]['id'];
162 // Update Architecture id
163 commitUpdateAttrValue ($object_id = $newmachine, $attr_id = $id, $value = $facter['architecture']);
164 }
165
166
167
168 // Find Memory id
169 $query = "select id from Attribute where name='DRAM, GIB' LIMIT 1";
170 unset($result);
171 $result = usePreparedSelectBlade ($query);
172 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
173 if($resultarray) {
174 $id=$resultarray[0]['id'];
175 // Update Memory id
176 commitUpdateAttrValue ($object_id = $newmachine, $attr_id = $id, $value = $facter['memorysize']);
177 }
178
179
180 // Find CPU id (custom field you'll need to add yourself)
181 $cpu = $facter['processorcount'] . " x " . $facter['processor0'];
182 $query = "select id from Attribute where name='CPU' LIMIT 1";
183 unset($result);
184 $result = usePreparedSelectBlade ($query);
185 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
186 if($resultarray) {
187 $id=$resultarray[0]['id'];
188 // Update CPU id
189 $cpu = $facter['processorcount'] . " x " . $facter['processor0'];
190 commitUpdateAttrValue ($object_id = $newmachine, $id, $value = $cpu);
191 }
192
193 // Add network interfaces
194
195 // Create an array with interfaces
196 $nics = explode(',',$facter['interfaces']);
197
198 // Go thew all interfaces and add IP and MAC
199 $count = count($nics);
200 for ($i = 0; $i < $count; $i++) {
201 // Remove newline from the field
202 $nics[$i]=str_replace("\n","", $nics[$i]);
203
204 // We generally don't monitor sit interfaces.
205 // We don't do this for lo interfaces, too
206 if ($nics[$i] == "sit0" || $nics[$i] == "lo")
207 break;
208
209 // Get IP
210 if (isset($facter['ipaddress_' . $nics[$i]]))
211 $ip = $facter['ipaddress_' . $nics[$i]];
212
213 // Get MAC
214 if (isset($facter['macaddress_' . $nics[$i]]))
215 $mac = $facter['macaddress_' . $nics[$i]];
216
217 //check if VM or not
218 if ($facter['is_virtual']=="false\n")
219 {
220 // Find 1000Base-T id
221 $query = "select dict_key from Dictionary where dict_value='1000Base-T' LIMIT 1";
222 }
223 else
224 {
225 // Find virtual port id
226 $query = "select dict_key from Dictionary where dict_value='virtual port' LIMIT 1";
227 }
228 unset($result);
229 $result = usePreparedSelectBlade ($query);
230 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
231 if($resultarray) {
232 $nictypeid=$resultarray[0]['dict_key'];
233 }
234
235 // Remove newline from ip
236 $ip=str_replace("\n","", $ip);
237
238 // Check to se if the interface has an ip assigned
239 $query = "SELECT object_id FROM IPv4Allocation where object_id=$newmachine and name=\"$nics[$i]\"";
240 unset($result);
241 $result = usePreparedSelectBlade ($query);
242 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
243
244 if($resultarray) {
245 unset($id);
246 $ipcheck=$resultarray;
247 }
248
249
250 // Check if it's been configured a port already
251 $query = "SELECT id FROM Port where object_id=$newmachine and name=\"$nics[$i]\"";
252 unset($result);
253 $result = usePreparedSelectBlade ($query);
254 $resultarray = $result->fetchAll (PDO::FETCH_ASSOC);
255
256 if($resultarray) {
257 $portid = $resultarray[0]['id'];
258 unset($id);
259 $portcheck=$resultarray;
260 }
261
262 // Add/update port
263 if ( count($portcheck) == 1 ) {
264 commitUpdatePort($newmachine,$portid, $nics[$i], $nictypeid, "Ethernet port", "$mac", NULL);
265 }
266 else
267 {
268 commitAddPort($object_id = $newmachine, $nics[$i], $nictypeid,'Ethernet port',"$mac");
269 }
270
271 // Add/update ip
272 if (count($ipcheck) == 1 ) {
273 if( $ip ) {
274 updateAddress($ip , $newmachine, $nics[$i],'regular');
275 }
276 }
277 else
278 {
279 if( $ip ) {
280 bindIpToObject($ip , $newmachine, $nics[$i],'regular');
281 }
282 }
283
284 unset($portcheck);
285 unset($ipcheck);
286 unset($ip);
287 unset($mac);
288 }
289 return buildWideRedirectURL ();
38ba29a5
AD
290}
291
292// Display the import page.
293function ViewHTML()
294{
1806a4df
DO
295 startPortlet();
296
297 echo "<table with=90% align=center border=0 cellpadding=5 cellspacing=0 align=center class=cooltable><tr valign=top>";
298 echo "<form method=post enctype=\"multipart/form-data\" action='index.php?module=redirect&page=depot&tab=facter&op=Update'>";
299 echo "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"30000\" />";
300 echo "Upload a facter file: <input name=\"userfile\" type=\"file\" /><br />";
301 echo "<input type=\"submit\" value=\"Upload File\" />";
302
303 echo "</td></tr></table></td></tr>";
304 echo "</form>";
305 echo "</table>";
306 finishPortlet();
38ba29a5
AD
307}
308
309function getdict ($hw,$chapter) {
1806a4df
DO
310 try {
311 global $dbxlink;
312 $query = "select dict_key from Dictionary where chapter_id='$chapter' AND dict_value ='$hw' LIMIT 1";
313 $result = usePreparedSelectBlade ($query);
314 $array = $result->fetchAll (PDO::FETCH_ASSOC);
315
316 if($array) {
317 return $array[0]['dict_key'];
318 }
319 else {
320 // Chapter ID for hardware is 11.
321 $dbxlink->exec("INSERT INTO Dictionary (chapter_id,dict_value) VALUES ('$chapter','$hw')");
322
323 $squery = "select dict_key from Dictionary where dict_value ='$hw' AND chapter_ID ='$chapter' LIMIT 1";
324 $sresult = usePreparedSelectBlade ($squery);
325 $sarray = $sresult->fetchAll (PDO::FETCH_ASSOC);
326
327 if($sarray) {
328 return $sarray[0]['dict_key'];
329 }
330
331 else {
332 // If it still has not returned, we are up shit creek.
333 return 0;
334 }
335 }
336 $dbxlink = null;
337 }
338 catch(PDOException $e)
339 {
340 echo $e->getMessage();
341 }
38ba29a5
AD
342}
343?>