omit the PHP closing tag at EOF in all PHP files
[racktables] / wwwroot / inc / triggers.php
1 <?php
2
3 # This file is a part of RackTables, a datacenter and server room management
4 # framework. See accompanying file "COPYING" for the full copyright and
5 # licensing information.
6
7 /*
8 *
9 * This file is a library of tab triggers for RackTables.
10 *
11 */
12
13 // Triggers may be optionally referred by some tabs of a page.
14 // In case they are defined, the given tab is only displayed if
15 // the trigger returned CSS class name. In certain cases, a key is necessary
16 // to decide (the 'bypass' hint of a page), and in some cases,
17 // other data can be used.
18
19 // APC "switched rack PDU" stands for a number of part numbers:
20 // http://www.apc.com/products/family/index.cfm?id=70
21 $known_APC_SKUs = array
22 (
23 // 100V input
24 1151 => 'AP7902J',
25 1152 => 'AP7930J',
26 1153 => 'AP7932J',
27 // 120V input
28 1154 => 'AP7900',
29 1155 => 'AP7901',
30 1156 => 'AP7902',
31 1157 => 'AP7930',
32 1158 => 'AP7931',
33 1159 => 'AP7932',
34 // 208V input
35 1160 => 'AP7911',
36 1161 => 'AP7940',
37 1162 => 'AP7941',
38 // 208V 3 phases input
39 1163 => 'AP7960',
40 1164 => 'AP7961',
41 1165 => 'AP7968',
42 1166 => 'AP7990',
43 1167 => 'AP7991',
44 1168 => 'AP7998',
45 // 230V input
46 1137 => 'AP7920',
47 1138 => 'AP7921',
48 1139 => 'AP7922',
49 1140 => 'AP7950',
50 1141 => 'AP7951',
51 1142 => 'AP7952',
52 1143 => 'AP7953',
53 1144 => 'AP7954',
54 // 400V 3 phases input
55 1154 => 'AP7957',
56 );
57
58 // This trigger is on when any of the (get_mac_list, get_link_status) ops permitted
59 function trigger_liveports ()
60 {
61 $breed = detectDeviceBreed (getBypassValue());
62 foreach (array ('getportstatus' => 'get_link_status', 'getmaclist' => 'get_mac_list') as $command => $opname)
63 if
64 (
65 validBreedFunction ($breed, $command) &&
66 permitted (NULL, 'liveports', $opname)
67 )
68 return 'std';
69 return '';
70 }
71
72 // SNMP port finder tab trigger. At the moment we decide on showing it
73 // for pristine switches/PDUs only. Once a user has begun
74 // filling the data in, we stop showing the tab.
75 function trigger_snmpportfinder ()
76 {
77
78 $object = spotEntity ('object', getBypassValue());
79 switch ($object['objtype_id'])
80 {
81 case 7: // any router
82 case 8: // or switch
83 case 965: // or wireless device would suffice
84 return $object['nports'] ? '' : 'attn';
85 case 2: // but only selected PDUs
86 if ($object['nports'])
87 return '';
88 global $known_APC_SKUs;
89 return checkTypeAndAttribute
90 (
91 $object['id'],
92 2, // PDU
93 2, // HW type
94 array_keys ($known_APC_SKUs)
95 ) ? 'attn' : '';
96 default:
97 return '';
98 }
99 }
100
101 function trigger_isloadbalancer ()
102 {
103 return considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'IPV4LB_LISTSRC') ? 'std' : '';
104 }
105
106 function trigger_ip ()
107 {
108 if (count (getObjectIPAllocationList (getBypassValue())))
109 return 'std';
110 // Only hide the tab, if there are no addresses allocated.
111 return considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'IPV4OBJ_LISTSRC') ? 'std' : '';
112 }
113
114 function trigger_natv4 ()
115 {
116 if (!count (getObjectIPv4AllocationList (getBypassValue())))
117 return '';
118 return considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'IPV4NAT_LISTSRC') ? 'std' : '';
119 }
120
121 function trigger_autoports ()
122 {
123 $object_id = getBypassValue();
124 if (0 != getPortsCount ($object_id))
125 return '';
126 $object = spotEntity ('object', $object_id);
127 return count (getAutoPorts ($object)) ? 'attn' : '';
128 }
129
130 function trigger_tags ()
131 {
132 global $taglist;
133 return count ($taglist) ? 'std' : '';
134 }
135
136 function trigger_passwdchange ()
137 {
138 global $user_auth_src, $remote_username;
139 return ($user_auth_src == 'database' || 1 === getUserIDByUsername ($remote_username)) ? 'std' : '';
140 }
141
142 function trigger_localreports ()
143 {
144 global $localreports;
145 return count ($localreports) ? 'std' : '';
146 }
147
148 function trigger_file_editText ()
149 {
150 $fileInfo = spotEntity ('file', getBypassValue());
151 return ($fileInfo['type'] == 'text/plain') ? 'std' : '';
152 }
153
154 function trigger_rackspace ()
155 {
156 global $virtual_obj_types;
157
158 // Hide the tab if the object type is virtual
159 $object = spotEntity ('object', getBypassValue());
160 if (in_array($object['objtype_id'], $virtual_obj_types))
161 return '';
162
163 // Show tab if the object is already mounted
164 return ($object['rack_id'] || getEntitiesCount ('rack') > 0) ? 'std' : '';
165 }
166
167 function trigger_ports ()
168 {
169 // Hide the tab if the object type exists in the exclusion config option
170 if (considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'PORT_EXCLUSION_LISTSRC'))
171 return '';
172
173 return 'std';
174 }
175
176 // Offer the generic VLAN setup tab for every object that already
177 // has a VLAN domain associated or at least can have one (in the latter
178 // case additionally heat the tab, if no domain is set.
179 function trigger_object_8021qorder ()
180 {
181 if (NULL !== getVLANSwitchInfo (getBypassValue()))
182 return 'std';
183 if (! count (getVLANDomainOptions()) || ! getEntitiesCount ('vst'))
184 return '';
185 if (considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'VLANSWITCH_LISTSRC'))
186 return 'attn';
187 return '';
188 }
189
190 function trigger_8021q_configured ()
191 {
192 return (count (getVLANDomainOptions()) && getEntitiesCount ('vst')) ? 'std' : '';
193 }
194
195 // implement similar logic for IPv4 networks
196 function trigger_ipv4net_vlanconfig ()
197 {
198 if (!count (getVLANDomainOptions())) // no domains -- no VLANs to bind with
199 return '';
200 $netinfo = spotEntity ('ipv4net', getBypassValue());
201 if ($netinfo['vlanc'])
202 return 'std';
203 return considerConfiguredConstraint ($netinfo, 'VLANIPV4NET_LISTSRC') ? 'attn' : '';
204 }
205
206 // implement similar logic for IPv6 networks
207 function trigger_ipv6net_vlanconfig ()
208 {
209 if (!count (getVLANDomainOptions())) // no domains -- no VLANs to bind with
210 return '';
211 $netinfo = spotEntity ('ipv6net', getBypassValue());
212 if ($netinfo['vlanc'])
213 return 'std';
214 return considerConfiguredConstraint ($netinfo, 'VLANIPV4NET_LISTSRC') ? 'attn' : '';
215 }
216
217 function trigger_vlan_ipv4net ()
218 {
219 $vlan_info = getVLANInfo (getBypassValue());
220 return count ($vlan_info['ipv4nets']) ? 'std' : 'attn';
221 }
222
223 function trigger_vlan_ipv6net ()
224 {
225 $vlan_info = getVLANInfo (getBypassValue());
226 return count ($vlan_info['ipv6nets']) ? 'std' : 'attn';
227 }
228
229 function trigger_object_8021qports ()
230 {
231 if (NULL === getVLANSwitchInfo (getBypassValue()))
232 return '';
233 return count (getStored8021QConfig (getBypassValue(), 'desired')) ? 'std' : '';
234 }
235
236 function trigger_object_8021qsync ()
237 {
238 if (NULL === $vswitch = getVLANSwitchInfo (getBypassValue()))
239 return '';
240 return $vswitch['out_of_sync'] == 'yes' ? 'attn' : 'std';
241 }
242
243 function trigger_LiveCDP ()
244 {
245 return trigger_anyDP ('getcdpstatus', 'CDP_RUNNERS_LISTSRC');
246 }
247
248 function trigger_LiveLLDP ()
249 {
250 return trigger_anyDP ('getlldpstatus', 'LLDP_RUNNERS_LISTSRC');
251 }
252
253 function trigger_anyDP ($command, $constraint)
254 {
255 if
256 (
257 validBreedFunction (detectDeviceBreed (getBypassValue()), $command) &&
258 considerConfiguredConstraint (spotEntity ('object', getBypassValue()), $constraint)
259 )
260 return 'std';
261 return '';
262 }
263
264 // tease rules editor tab, when the VST has no rules
265 function trigger_vst_editrules()
266 {
267 $vst = spotEntity ('vst', getBypassValue());
268 return $vst['rulec'] ? 'std' : 'attn';
269 }
270
271 function triggerIPAddressLog ()
272 {
273 $ip_bin = getBypassValue();
274 switch (strlen ($ip_bin))
275 {
276 case 4:
277 $result = usePreparedSelectBlade ("SELECT COUNT(id) FROM IPv4Log WHERE ip = ?", array (ip4_bin2db ($ip_bin)));
278 break;
279 case 16:
280 $result = usePreparedSelectBlade ("SELECT COUNT(id) FROM IPv6Log WHERE ip = ?", array ($ip_bin));
281 break;
282 }
283 if ($row = $result->fetch(PDO::FETCH_NUM))
284 if ($row[0] > 0)
285 return 'std';
286 return '';
287 }
288
289 function triggerCactiGraphs ()
290 {
291 if (! count (getCactiServers()))
292 return '';
293 if
294 (
295 count (getCactiGraphsForObject (getBypassValue())) ||
296 considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'CACTI_LISTSRC')
297 )
298 return 'std';
299 else
300 return '';
301 }
302
303 function triggerMuninGraphs()
304 {
305 if (! count (getMuninServers()))
306 return '';
307 if
308 (
309 count (getMuninGraphsForObject (getBypassValue())) ||
310 considerConfiguredConstraint (spotEntity ('object', getBypassValue()), 'MUNIN_LISTSRC')
311 )
312 return 'std';
313 else
314 return '';
315 }
316
317 function trigger_ucs()
318 {
319 return checkTypeAndAttribute
320 (
321 getBypassValue(),
322 1787, # management interface
323 30, # mgmt type
324 array (1788) # UCS Manager
325 ) ? 'std' : '';
326 }
327
328 function triggerPatchCableHeapsConfigured()
329 {
330 return count (getPatchCableHeapSummary()) ? 'std' : '';
331 }
332
333 function triggerGraphCycleResolver()
334 {
335 global $pageno;
336 switch ($pageno)
337 {
338 case 'tagtree':
339 global $taglist;
340 $nodelist = $taglist;
341 break;
342 default:
343 throw new RackTablesError ('unexpected call to trigger function', RackTablesError::INTERNAL);
344 }
345 return count (getInvalidNodes ($nodelist)) ? 'attn' : '';
346 }