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