Fix checkRpItemsPosition
[ryzomcore.git] / web / public_php / admin / functions_tool_main.php
blob97d833fd3748c07820cbaa8b3f788f1010bee4ad
1 <?php
3 $refresh_rates = array(
4 array('desc' => 'Every 5 secs',
5 'secs' => 5,
6 ),
7 array('desc' => 'Every 30 secs',
8 'secs' => 30,
9 ),
10 array('desc' => 'Every 1 min.',
11 'secs' => 60,
13 array('desc' => 'Every 5 min.',
14 'secs' => 300,
16 array('desc' => 'Every 10 min.',
17 'secs' => 600,
19 array('desc' => 'Every 30 min.',
20 'secs' => 1800,
22 array('desc' => 'Never',
23 'secs' => 0,
27 class MyAdminService extends CAdminServiceWeb
29 function commandResult($serviceModuleName, $result)
31 global $tpl;
32 global $command_return_data;
34 nt_common_add_debug("Service $serviceModuleName returned '$result'");
36 $data = "===[ Service ". strtoupper($serviceModuleName) ." returned ]===\n". trim($result) ."\n\n";
38 if (isset($command_return_data)) $command_return_data[] = explode("\n", $data);
39 $tpl->append('tool_execute_result', $data);
42 function invokeError($methodName, $errorString)
44 global $tpl;
46 nt_common_add_debug("AS Error in '$methodName' : $errorString");
47 $tpl->assign('tool_as_error', "AS Error in '$methodName' : $errorString");
52 function tool_main_check_user_domain($domain_id)
54 global $nel_user;
56 if (!$domain_id) return false;
58 $ud = $nel_user['access']['domains'];
60 if (is_array($ud))
62 reset($ud);
63 foreach($ud as $udomain)
65 if ($domain_id == $udomain['domain_id']) return true;
69 return false;
72 function tool_main_check_user_shard($shard_id)
74 global $nel_user;
76 if (!$shard_id) return false;
78 $us = $nel_user['access']['shards'];
80 if (is_array($us))
82 reset($us);
83 foreach($us as $ushard)
85 if ($shard_id == $ushard['shard_id']) return true;
89 return false;
92 function tool_main_get_domain_name($domain_id)
94 global $nel_user;
96 reset($nel_user['access']['domains']);
97 foreach($nel_user['access']['domains'] as $domain)
99 if ($domain['domain_id'] == $domain_id)
101 return $domain['domain_name'];
105 return null;
108 function tool_main_get_domain_data($domain_id, $field)
110 global $nel_user;
111 reset($nel_user['access']['domains']);
112 foreach($nel_user['access']['domains'] as $domain)
114 if ($domain['domain_id'] == $domain_id)
116 // echo "ok $domain_id : $field : ".$domain[$field];
117 return $domain[$field];
120 return null;
124 function tool_main_get_domain_rrd_path($domain_id)
126 global $nel_user;
128 reset($nel_user['access']['domains']);
129 foreach($nel_user['access']['domains'] as $domain)
131 if ($domain['domain_id'] == $domain_id)
133 return $domain['domain_rrd_path'];
137 return null;
140 function tool_main_get_shard_name($shard_id)
142 global $nel_user;
144 reset($nel_user['access']['shards']);
145 foreach($nel_user['access']['shards'] as $shard)
147 if ($shard['shard_id'] == $shard_id)
149 return $shard['shard_name'];
153 return null;
156 function tool_main_get_shard_as_id($shard_id)
158 global $nel_user;
160 reset($nel_user['access']['shards']);
161 foreach($nel_user['access']['shards'] as $shard)
163 if ($shard['shard_id'] == $shard_id)
165 return $shard['shard_as_id'];
169 return null;
172 function tool_main_get_shard_data($shard_id, $field)
174 global $nel_user;
176 reset($nel_user['access']['shards']);
177 foreach($nel_user['access']['shards'] as $shard)
179 if ($shard['shard_id'] == $shard_id)
181 return $shard[$field];
185 return null;
189 function tool_main_get_domain_host($domain_id)
191 global $nel_user;
193 reset($nel_user['access']['domains']);
194 foreach($nel_user['access']['domains'] as $domain)
196 if ($domain['domain_id'] == $domain_id)
198 return $domain['domain_as_host'];
202 return null;
205 function tool_main_get_domain_port($domain_id)
207 global $nel_user;
209 reset($nel_user['access']['domains']);
210 foreach($nel_user['access']['domains'] as $domain)
212 if ($domain['domain_id'] == $domain_id)
214 return $domain['domain_as_port'];
218 return null;
221 function tool_main_get_elapsed_time_string($seconds, &$string)
223 //returns an array of numeric values representing days, hours, minutes & seconds respectively
224 $ret=array('days'=>0,'hours'=>0,'minutes'=>0,'seconds'=>0,'totalseconds'=>0);
226 $totalsec = $seconds;
227 $ret['totalseconds'] = $totalsec;
228 //print $earlierDate. ":". $laterDate. ":". $totalsec ."<br>";
230 if ($totalsec >= 86400)
232 $ret['days'] = floor($totalsec/86400);
233 $totalsec = $totalsec % 86400;
235 if ($totalsec >= 3600)
237 $ret['hours'] = floor($totalsec/3600);
238 $totalsec = $totalsec % 3600;
240 if ($totalsec >= 60)
242 $ret['minutes'] = floor($totalsec/60);
244 $ret['seconds'] = $totalsec % 60;
246 $string = '';
247 $string .= ($ret['days'] > 0) ? tool_main_leftpad($ret['days']).'d ' : '';
248 $string .= (($ret['hours'] > 0) || (strlen($string) > 0)) ? tool_main_leftpad($ret['hours']).'h ' : '';
249 $string .= (($ret['minutes'] > 0) || (strlen($string) > 0)) ? tool_main_leftpad($ret['minutes']).'m ' : '';
250 $string .= (($ret['seconds'] > 0) || (strlen($string) > 0)) ? tool_main_leftpad($ret['seconds']).'s ' : '';
252 $ret['string'] = trim($string);
254 return $ret;
257 function tool_main_leftpad($n,$cc=2,$ch='0')
259 return str_pad($n, $cc, $ch, STR_PAD_LEFT);
262 function tool_main_get_shards_from_status($status, $filters)
264 $result = array();
266 if (is_array($status) && sizeof($status))
268 reset($status);
269 foreach($status as $sline)
271 $shard_name = trim($sline['ShardName']);
273 if (($shard_name != '') && (isset($filters[$shard_name]) || isset($filters['_all_'])))
275 $result[] = $shard_name;
278 $result = array_values(array_unique($result));
281 return $result;
284 function tool_main_get_aes_from_status($status)
286 $result = array();
287 if (is_array($status) && sizeof($status))
289 reset($status);
290 foreach($status as $skey => $sline)
292 $short_name = trim($sline['ShortName']);
293 $running_state = trim($sline['RunningState']);
294 if (($short_name == 'AES') && ($running_state == 'online'))
296 $result[] = $sline['AliasName'];
300 return $result;
303 function tool_main_parse_status($status)
305 $check_graphs = tool_admin_applications_check('tool_main_graphs');
307 $_sort_list = array();
308 $domainServices = array();
309 $sortedServices = array();
310 if (is_array($status) && sizeof($status))
312 reset($status);
313 foreach($status as $sline)
315 $vars = array();
316 $vars['_flags_'] = array();
318 $sline_vars = explode("\t", $sline);
319 reset($sline_vars);
320 foreach($sline_vars as $sline_var)
322 $sline_parts = explode("=", $sline_var);
323 if (!isset($sline_parts[1])) {
324 $sline_parts[1] = '';
327 if ($sline_parts[0] == 'RunningState')
329 // this is a small fix to an unknown server bug :)
330 if (trim($sline_parts[1]) == 'topped') $sline_parts[1] = 'rs_stopped';
331 elseif (trim($sline_parts[1]) == 'nline') $sline_parts[1] = 'rs_online';
333 $vars['_flags_'][$sline_parts[1]] = true;
334 $sline_parts[1] = substr($sline_parts[1], 3);
336 elseif ($sline_parts[0] == 'RunningOrders')
338 $vars['_flags_'][$sline_parts[1]] = true;
339 $sline_parts[1] = substr($sline_parts[1], 3);
341 elseif ($sline_parts[0] == 'RunningTags')
343 $_tmp = explode(" ", trim($sline_parts[1]));
344 reset($_tmp);
345 foreach($_tmp as $_tmp_key => $_tmp_part)
347 $vars['_flags_'][$_tmp_part] = true;
348 $_tmp_part = str_replace('_',' ',substr($_tmp_part, 3));
349 $_tmp[$_tmp_key] = $_tmp_part;
351 $sline_parts[1] = implode(' / ', $_tmp);
353 elseif ($sline_parts[0] == 'NoReportSince')
355 $_today = time();
356 $_day = date("d", $_today);
357 $_month = date("m", $_today);
358 $_year = date("Y", $_today);
359 $_base_day = mktime(0, 0, 0, $_month, $_day, $_year);
361 if ($sline_parts[1] >= $_base_day)
363 $sline_parts[1] = 'n/a';
365 else
367 $time0 = 0 + $sline_parts[1]; // convert to a number ;)
368 if ($time0 > 60) $vars['_flags_']['alert_red'] = true;
369 elseif ($time0 > 40) $vars['_flags_']['alert_orange_dark'] = true;
370 elseif ($time0 > 25) $vars['_flags_']['alert_orange_light'] = true;
372 tool_main_get_elapsed_time_string($sline_parts[1],$sline_parts[1]);
375 elseif ($sline_parts[0] == 'UpTime')
377 $_today = time();
378 $_day = date("d", $_today);
379 $_month = date("m", $_today);
380 $_year = date("Y", $_today);
381 $_base_day = mktime(0, 0, 0, $_month, $_day, $_year);
383 if ($sline_parts[1] >= $_base_day)
385 $sline_parts[1] = 'n/a';
387 else
389 tool_main_get_elapsed_time_string($sline_parts[1],$sline_parts[1]);
392 elseif ($sline_parts[0] == 'State')
394 if (strtolower($sline_parts[1]) == 'stalled') $vars['_flags_']['alert_red'] = true;
395 elseif (strtolower($sline_parts[1]) == 'halted') $vars['_flags_']['alert_red'] = true;
396 $vars['_flags_'][$sline_parts[1]] = true;
398 elseif ($sline_parts[0] == 'Hostname')
400 $sline_parts[1] = substr($sline_parts[1], 0, strpos($sline_parts[1], '.'));
402 elseif ($sline_parts[0] == 'AliasName')
404 $vars['_flags_']['alias_code'] = '';
407 $vars[$sline_parts[0]] = $sline_parts[1];
410 // check is service is chain crashing
411 if (in_array('rt_chain_crashing', array_keys($vars['_flags_'])))
413 // check is service is online (anything that is not stopped)
414 if (!in_array('rs_stopped', array_keys($vars['_flags_'])))
416 // check the start counts for crashing age
417 $crash_counts = explode(' ', $vars['StartCounter']);
418 if ($crash_counts[0] >= 5) $vars['_flags_']['alert_red'] = true;
419 elseif ($crash_counts[1] >= 5) $vars['_flags_']['alert_orange_dark'] = true;
420 elseif ($crash_counts[2] >= 5) $vars['_flags_']['alert_orange_light'] = true;
424 //$vars['_flags_']['has_graphs'] = tool_main_check_rrd_files($vars['AliasName']);
425 $_sort_list[] = $vars['AliasName'];
427 $domainServices[] = $vars;
430 sort($_sort_list);
432 reset($_sort_list);
433 foreach($_sort_list as $_sort_name)
435 reset($domainServices);
436 foreach($domainServices as $serviceRow)
438 if ($_sort_name == $serviceRow['AliasName'])
440 $sortedServices[] = $serviceRow;
441 break;
447 return $sortedServices;
450 function tool_main_check_rrd_files($service)
452 if ($handle = opendir(NELTOOL_RRDBASE))
454 while (false !== ($file = readdir($handle)))
456 if ($file != "." && $file != "..")
458 $file_parts = explode(".", $file);
459 if (sizeof($file_parts) == 3)
461 if (($service == $file_parts[0]) && (($file_parts[2] == 'rrd') || ($file_parts[2] == 'hrd')))
463 closedir($handle);
464 return true;
469 closedir($handle);
472 return false;
475 function tool_main_get_checked_services()
477 global $NELTOOL;
479 $services = array();
481 reset($NELTOOL['POST_VARS']);
482 foreach($NELTOOL['POST_VARS'] as $post_key => $post_val)
484 $val = 'service_'. $post_val;
485 if ($post_key == $val)
487 $services[] = $post_val;
491 return $services;
494 function tool_main_get_shard_ids($shard_id)
496 global $nel_user;
498 $data = array();
499 reset($nel_user['access']['shards']);
500 foreach($nel_user['access']['shards'] as $shards)
502 if ($shards['shard_id'] == $shard_id)
504 $shards_tmp = trim($shards['shard_as_id']);
505 if ($shards_tmp == '*')
507 $data['_all_'] = true;
509 elseif ($shards_tmp == '?')
511 $data['_unknown_'] = true;
513 else
515 $shard_parts = explode(':', $shards_tmp);
516 reset($shard_parts);
517 foreach($shard_parts as $shard_as_id)
519 if ($shard_as_id == '?')
521 $data['_unknown_'] = true;
523 else
525 $data[$shard_as_id] = true;
533 return $data;
536 function tool_main_get_shards_orders($orders)
538 $data = array();
540 if (is_array($orders) && sizeof($orders))
542 reset($orders);
543 foreach($orders as $order_line)
545 $order_items = explode("\t", $order_line);
547 $shard_name = "";
548 $shard_order = "";
550 reset($order_items);
551 foreach($order_items as $order_parts)
553 $order_bits = explode("=", $order_parts);
555 $order_bits[0] = trim($order_bits[0]);
556 $order_bits[1] = trim($order_bits[1]);
558 if ($order_bits[0] == 'ShardName')
560 $shard_name = $order_bits[1];
562 elseif ($order_bits[0] == 'Orders')
564 $shard_order = substr($order_bits[1],3);
568 if ($shard_name != "" && $shard_order != "")
570 $data[$shard_name] = $shard_order;
576 return $data;
579 function tool_main_get_last_hd_time_for_domain($domain_id)
581 global $db;
583 $timer = 0;
585 $sql = "SELECT * FROM ". NELDB_STAT_HD_TIME_TABLE ." WHERE hd_domain_id=". $domain_id;
586 if ($result = $db->sql_query($sql))
588 if ($db->sql_numrows($result))
590 $data = $db->sql_fetchrow($result);
591 $timer = $data['hd_last_time'];
595 return $timer;
598 function tool_main_update_hd_time_for_domain($domain_id, $now)
600 global $db;
602 //if ($stat_time == 0) $sql = "INSERT INTO ". NELDB_STAT_HD_TIME_TABLE ." (`hd_domain_id`,`hd_last_time`) VALUES (". $domain_id .",". $now .")";
603 //else $sql = "UPDATE ". NELDB_STAT_HD_TIME_TABLE ." SET hd_last_time=". $now ." WHERE hd_domain_id=". $domain_id;
605 $sql = "SELECT * FROM ". NELDB_STAT_HD_TIME_TABLE ." WHERE hd_domain_id=". $domain_id;
606 if ($result = $db->sql_query($sql))
608 if ($db->sql_numrows($result))
610 $sql = "UPDATE ". NELDB_STAT_HD_TIME_TABLE ." SET hd_last_time=". $now ." WHERE hd_domain_id=". $domain_id;
611 $db->sql_query($sql);
613 else
615 $sql = "INSERT INTO ". NELDB_STAT_HD_TIME_TABLE ." (`hd_domain_id`,`hd_last_time`) VALUES (". $domain_id .",". $now .")";
616 $db->sql_query($sql);
621 function tool_main_update_hd_data_for_domain($domain_id, $data)
623 global $db;
625 $aes_status = array();
627 if (is_array($data) && sizeof($data))
629 reset($data);
630 foreach($data as $aes)
632 $aes_server = null;
634 //$hd_found_slash_mount = null;
635 //$hd_found_slash_home = null;
636 //$hd_found_slash_home_nevrax = null;
637 //$hd_found_slash = null;
639 $aes_hd_ready = false;
641 $aes_data = explode("\n", $aes);
642 reset($aes_data);
643 foreach($aes_data as $aes_line)
645 $aes_line = trim($aes_line);
647 // clean up the multiple blank characters
648 $aes_line = ereg_replace("[[:space:]]+"," ",$aes_line);
650 if (ereg("^===\[ Service AES_([^[:space:]]+) returned \]===$", $aes_line, $regs))
652 $aes_server = strtolower($regs[1]);
653 $aes_status[$aes_server] = array();
655 elseif (ereg("^-* Command output begin -*$", $aes_line, $regs))
657 $aes_hd_ready = true;
659 elseif (ereg("^-* Command output end -*$", $aes_line, $regs))
661 $aes_hd_ready = false;
664 if ($aes_hd_ready)
666 $aes_line_data = explode(" ", $aes_line);
668 if (($aes_line_data[0] != 'Filesystem') && ($aes_line_data[1] != 'Command'))
670 //if ((strpos($aes_line_data[5],"/mnt/rsbk") == 0)
671 // || ($aes_line_data[5] == "/home")
672 // || ($aes_line_data[5] == "/home/nevrax")
673 // || ($aes_line_data[5] == "/"))
675 $aes_status[$aes_server][] = array( 'device' => $aes_line_data[0],
676 'size' => $aes_line_data[1],
677 'used' => $aes_line_data[2],
678 'free' => $aes_line_data[3],
679 'usedpercent' => str_replace('%','',$aes_line_data[4]),
680 'mount' => $aes_line_data[5],
688 //if ((substr($aes_line, 0, 5) == "/dev/") && $aes_server)
691 // $aes_line_data = explode(" ", $aes_line);
693 // // 0 : device name
694 // // 1 : device size
695 // // 2 : used
696 // // 3 : available
697 // // 4 : used percentage
698 // // 5 : mount point
700 // if ($aes_line_data[5] == "/home")
701 // {
702 // $hd_found_slash_home = $aes_line_data;
703 // }
705 // if ($aes_line_data[5] == "/home/nevrax")
706 // {
707 // $hd_found_slash_home_nevrax = $aes_line_data;
708 // }
710 // if ($aes_line_data[5] == "/")
711 // {
712 // $hd_found_slash = $aes_line_data;
713 // }
715 // //if (($aes_line_data[5] == "/") || ($aes_line_data[5] == "/home/nevrax"))
716 // //{
717 // // $aes_status[$aes_server][] = array( 'device' => $aes_line_data[0],
718 // // 'size' => $aes_line_data[1],
719 // // 'used' => $aes_line_data[2],
720 // // 'free' => $aes_line_data[3],
721 // // 'usedpercent' => str_replace('%','',$aes_line_data[4]),
722 // // 'mount' => $aes_line_data[5],
723 // // );
724 // //}
729 //$tmp = null;
731 //if (is_array($hd_found_slash_mount)) $tmp = $hd_found_slash_mount;
732 //elseif (is_array($hd_found_slash_home)) $tmp = $hd_found_slash_home;
733 //elseif (is_array($hd_found_slash_home_nevrax)) $tmp = $hd_found_slash_home_nevrax;
734 //elseif (is_array($hd_found_slash)) $tmp = $hd_found_slash;
736 //if (is_array($tmp))
738 // $aes_status[$aes_server][] = array( 'device' => $tmp[0],
739 // 'size' => $tmp[1],
740 // 'used' => $tmp[2],
741 // 'free' => $tmp[3],
742 // 'usedpercent' => str_replace('%','',$tmp[4]),
743 // 'mount' => $tmp[5],
744 // );
748 if (sizeof($aes_status))
750 nt_common_add_debug($aes_status);
751 //echo '<pre>'. print_r($aes_status, true) .'</pre>';
754 $sql = "DELETE FROM ". NELDB_STAT_HD_TABLE ." WHERE hd_domain_id=". $domain_id;
755 $db->sql_query($sql);
757 reset($aes_status);
758 foreach($aes_status as $server_name => $server_datas)
760 reset($server_datas);
761 foreach($server_datas as $server_hd)
763 if (trim($server_name) != '')
765 $sql = "INSERT INTO ". NELDB_STAT_HD_TABLE ." (`hd_domain_id`,`hd_server`,`hd_device`,`hd_size`,`hd_used`,`hd_free`,`hd_percent`,`hd_mount`)";
766 $sql .= " VALUES (". $domain_id .",'". $server_name ."','". $server_hd['device'] ."','". $server_hd['size'] ."',";
767 $sql .= "'". $server_hd['used'] ."','". $server_hd['free'] ."','". $server_hd['usedpercent'] ."','". $server_hd['mount'] ."')";
768 $db->sql_query($sql);
770 if (defined('NELTOOL_CRON_DEBUG')) echo "<br />SQL:$sql";
774 tool_main_update_hd_time_for_domain($domain_id, time());
780 function tool_main_get_hd_data_for_domain($domain_id)
782 global $db;
784 $data = array();
786 //$sql = "SELECT * FROM ". NELDB_STAT_HD_TABLE ." WHERE hd_domain_id=". $domain_id ." AND hd_mount='/' ORDER BY hd_percent DESC";
787 $sql = "SELECT * FROM ". NELDB_STAT_HD_TABLE ." WHERE hd_domain_id=". $domain_id ." ORDER BY hd_percent DESC";
788 if ($result = $db->sql_query($sql))
790 if ($db->sql_numrows($result))
792 while ($row = $db->sql_fetchrow($result))
794 $outp = '<b>'. $row['hd_server'] .'</b> : '. $row['hd_device'] .'<br>';
795 $outp .= '• Mount : '. $row['hd_mount'] .'<br>';
796 $outp .= '• Size : '. $row['hd_size'] .'<br>';
797 $outp .= '• Used : '. $row['hd_used'] .'<br>';
798 $outp .= '• Free : '. $row['hd_free'] .'<br>';
800 $row['summary'] = $outp;
801 $data[] = $row;
806 return $data;
809 function tool_main_get_annotation($domain_id, $shard_id)
811 global $db;
813 $data = null;
815 $sql = "SELECT * FROM ". NELDB_ANNOTATION_TABLE ." WHERE 1=0";
816 if ($domain_id > 0) $sql .= " OR annotation_domain_id=". $domain_id;
817 if ($shard_id > 0) $sql .= " OR annotation_shard_id=". $shard_id;
818 $sql .= " ORDER BY annotation_date DESC";
820 if ($result = $db->sql_query($sql))
822 if ($db->sql_numrows($result))
824 $data = $db->sql_fetchrow($result);
828 return $data;
831 function tool_main_get_lock($domain_id, $shard_id)
833 global $db;
835 $data = null;
837 $sql = "SELECT * FROM ". NELDB_LOCK_TABLE ." WHERE 1=0 ";
838 if ($domain_id > 0) $sql .= " OR lock_domain_id=". $domain_id;
839 if ($shard_id > 0) $sql .= " OR lock_shard_id=". $shard_id;
840 $sql .= " ORDER BY lock_date DESC";
842 if ($result = $db->sql_query($sql))
844 if ($db->sql_numrows($result))
846 $data = $db->sql_fetchrow($result);
850 return $data;
853 function tool_main_delete_lock_shard($shard_id)
855 global $db;
856 global $nel_user;
857 global $AS_Name, $AS_ShardName;
859 nt_log("Shard Unlock (Domain: '". $AS_Name ."' - Shard: '". $AS_ShardName ."') by '". $nel_user['user_name'] ."'");
860 $sql = "DELETE FROM ". NELDB_LOCK_TABLE ." WHERE lock_shard_id=". $shard_id;
861 $db->sql_query($sql);
864 function tool_main_delete_lock_domain($domain_id)
866 global $db;
867 global $nel_user;
868 global $AS_Name, $AS_ShardName;
870 nt_log("Domain Unlock (Domain: '". $AS_Name ."') by '". $nel_user['user_name'] ."'");
871 $sql = "DELETE FROM ". NELDB_LOCK_TABLE ." WHERE lock_domain_id=". $domain_id;
872 $db->sql_query($sql);
876 function tool_main_set_lock_shard($domain_id, $shard_id, $log=true)
878 global $db;
879 global $nel_user;
880 global $AS_Name, $AS_ShardName;
882 // we need to check if the shard is *all*
883 // if its the case, we go for a lock domain instead
885 $sql = "SELECT * FROM ". NELDB_SHARD_TABLE ." WHERE shard_id=". $shard_id;
886 if ($result = $db->sql_query($sql))
888 if ($db->sql_numrows($result))
890 $shard_row = $db->sql_fetchrow($result);
892 if ($shard_row['shard_as_id'] == "*")
894 return tool_main_set_lock_domain($domain_id, $log);
899 $data = tool_main_get_lock($domain_id, $shard_id);
900 $now = time();
902 if (is_array($data) && ($data['lock_shard_id'] > 0))
904 if ($log) nt_log("Shard Lock (Domain: '". $AS_Name ."' - Shard: '". $AS_ShardName ."') by '". $nel_user['user_name'] ."'");
905 $sql = "UPDATE ". NELDB_LOCK_TABLE ." SET lock_user_name='". $nel_user['user_name'] ."',lock_update=". $now ." WHERE lock_id=". $data['lock_id'];
906 $db->sql_query($sql);
908 elseif (!$data)
910 if ($log) nt_log("Shard Lock (Domain: '". $AS_Name ."' - Shard: '". $AS_ShardName ."') by '". $nel_user['user_name'] ."'");
911 $sql = "INSERT INTO ". NELDB_LOCK_TABLE ." (`lock_shard_id`,`lock_user_name`,`lock_date`,`lock_update`) VALUES (". $shard_id .",'". $nel_user['user_name'] ."',". $now .",". $now .")";
912 $db->sql_query($sql);
917 function tool_main_get_domain_shard_list($domain_id)
919 global $db;
921 $data = array();
923 $sql = "SELECT * FROM ". NELDB_SHARD_TABLE ." WHERE shard_domain_id=". $domain_id;
924 if ($result = $db->sql_query($sql))
926 if ($db->sql_numrows($result))
928 while ($row = $db->sql_fetchrow($result))
930 $data[] = $row['shard_id'];
935 return $data;
938 function tool_main_set_lock_domain($domain_id, $log=true)
940 global $db;
941 global $nel_user;
942 global $AS_Name, $AS_ShardName;
944 // to lock a domain you need to remove all locks from any shard in it
946 $lock_data = tool_main_get_lock($domain_id, 0);
948 $now = time();
950 if (is_array($lock_data))
952 $sql = "UPDATE ". NELDB_LOCK_TABLE ." SET lock_user_name='". $nel_user['user_name'] ."',lock_update=". $now ." WHERE lock_id=". $lock_data['lock_id'];
953 $db->sql_query($sql);
955 else
957 $shard_list = tool_main_get_domain_shard_list($domain_id);
958 $shard_list = array_values($shard_list);
960 $sql = "DELETE FROM ". NELDB_LOCK_TABLE ." WHERE lock_shard_id IN (". implode(',', $shard_list) .")";
961 $db->sql_query($sql);
963 $sql = "INSERT INTO ". NELDB_LOCK_TABLE ." (`lock_domain_id`,`lock_user_name`,`lock_date`,`lock_update`) VALUES (". $domain_id .",'". $nel_user['user_name'] ."',". $now .",". $now .")";
964 $db->sql_query($sql);
967 if ($log) nt_log("Domain Lock (Domain: '". $AS_Name ."') by '". $nel_user['user_name'] ."'");
970 function tool_main_set_annotation($domain_id, $shard_id, $annotation)
972 global $db;
973 global $nel_user;
974 global $AS_Name, $AS_ShardName;
976 $annotation = htmlentities(trim($annotation), ENT_QUOTES);
978 $data = tool_main_get_lock($domain_id, $shard_id);
980 if ($data['lock_domain_id'])
982 // its a domain lock
983 $shard_list = tool_main_get_domain_shard_list($domain_id);
984 $shard_list = array_values($shard_list);
986 $sql = "DELETE FROM ". NELDB_ANNOTATION_TABLE ." WHERE annotation_shard_id IN (". implode(',', $shard_list) .")";
987 $db->sql_query($sql);
989 $annotation_data = tool_main_get_annotation($domain_id, 0);
990 if ($annotation_data)
992 nt_log("Domain Annotation (Domain: '". $AS_Name ."') by '". $nel_user['user_name'] ."' : ". $annotation);
993 $sql = "UPDATE ". NELDB_ANNOTATION_TABLE ." SET annotation_data='". $annotation ."',annotation_user_name='". $nel_user['user_name'] ."',annotation_date=". time() ." WHERE annotation_id=". $annotation_data['annotation_id'];
994 $db->sql_query($sql);
996 else
998 nt_log("Domain Annotation (Domain: '". $AS_Name ."') by '". $nel_user['user_name'] ."' : ". $annotation);
999 $sql = "INSERT INTO ". NELDB_ANNOTATION_TABLE ." (`annotation_domain_id`,`annotation_data`,`annotation_user_name`,`annotation_date`) VALUES ";
1000 $sql .= "(". $domain_id .",'". $annotation ."','". $nel_user['user_name'] ."',". time() .")";
1001 $db->sql_query($sql);
1005 else
1007 // its a shard lock
1008 $annotation_data = tool_main_get_annotation(0, $shard_id);
1009 if ($annotation_data)
1011 nt_log("Shard Annotation (Domain: '". $AS_Name ."' - Shard: '". $AS_ShardName ."') by '". $nel_user['user_name'] ."' : ". $annotation);
1012 $sql = "UPDATE ". NELDB_ANNOTATION_TABLE ." SET annotation_data='". $annotation ."',annotation_user_name='". $nel_user['user_name'] ."',annotation_date=". time() ." WHERE annotation_id=". $annotation_data['annotation_id'];
1013 $db->sql_query($sql);
1015 else
1017 nt_log("Shard Annotation (Domain: '". $AS_Name ."' - Shard: '". $AS_ShardName ."') by '". $nel_user['user_name'] ."' : ". $annotation);
1018 $sql = "INSERT INTO ". NELDB_ANNOTATION_TABLE ." (`annotation_shard_id`,`annotation_data`,`annotation_user_name`,`annotation_date`) VALUES ";
1019 $sql .= "(". $shard_id .",'". $annotation ."','". $nel_user['user_name'] ."',". time() .")";
1020 $db->sql_query($sql);
1026 function tool_main_get_shards_info_from_db($application, $status, $filters, $ringsqlstring='')
1028 $shard_list = array();
1029 $shard_list2 = array();
1030 $shard_list_result = array();
1032 //nt_common_add_debug('in tool_main_get_shards_info_from_db()');
1033 //nt_common_add_debug($status);
1035 if (is_array($status) && sizeof($status))
1037 reset($status);
1038 foreach($status as $sline)
1040 $shard_name = trim($sline['ShardName']);
1041 $shard_id = isset($sline['ShardId']) ? trim($sline['ShardId']) : '';
1043 if (($shard_name != '' && $shard_id != '') && (isset($filters[$shard_name]) || isset($filters['_all_'])))
1045 if (!in_array($shard_name, array_values($shard_list)))
1047 $shard_list[$shard_id] = $shard_name;
1053 nt_common_add_debug('shard_list :');
1054 nt_common_add_debug($shard_list);
1056 reset($shard_list);
1057 foreach($shard_list as $shard_key => $shard_application)
1059 if (is_numeric($shard_key))
1061 $shard_list2[$shard_key] = $shard_application;
1065 //nt_common_add_debug('shard_list2 :');
1066 //nt_common_add_debug($shard_list2);
1068 if ($ringsqlstring == '')
1070 global $db;
1071 $db->sql_select_db('nel');
1073 //$sql = "SELECT * FROM shard WHERE ShardId IN (". implode(',', array_keys($shard_list)) .")";
1074 if (is_array($shard_list2) && sizeof($shard_list2))
1076 $sql = "SELECT * FROM shard, domain WHERE shard.domain_id=domain.domain_id AND shard.ShardId IN (". implode(',', array_keys($shard_list2)) .") AND domain.domain_name='". $application ."'";
1078 if ($result = $db->sql_query($sql))
1080 if ($db->sql_numrows($result))
1082 while ($row = $db->sql_fetchrow($result))
1084 // patch to support live and ats mysql changes
1085 $state = '';
1086 if (isset($row['State'])) $state = $row['State'];
1087 elseif (isset($row['RequiredState'])) $state = $row['RequiredState'];
1089 $shard_name = $shard_list[$row['ShardId']];
1090 $shard_list_result[$shard_name] = array('shard_id' => $row['ShardId'],
1091 'version' => $row['Version'],
1092 'state' => substr($state,3),
1093 'motd' => $row['MOTD'],
1100 $db->sql_reselect_db();
1102 else
1104 $db = new sql_db_string($ringsqlstring);
1105 if (is_object($db))
1107 if (is_array($shard_list2) && sizeof($shard_list2))
1109 //nt_common_add_debug("tool_main_get_shards_info_from_db()");
1110 //nt_common_add_debug($shard_list2);
1112 $sql = "SELECT * FROM shard WHERE shard_id IN (". implode(',', array_keys($shard_list2)) .")";
1114 if ($result = $db->sql_query($sql))
1116 if ($db->sql_numrows($result))
1118 while ($row = $db->sql_fetchrow($result))
1120 // patch to support live and ats mysql changes
1121 $state = '';
1122 if (isset($row['State'])) $state = $row['State'];
1123 elseif (isset($row['RequiredState'])) $state = $row['RequiredState'];
1125 $shard_name = $shard_list[$row['shard_id']];
1126 $shard_list_result[$shard_name] = array('shard_id' => $row['shard_id'],
1127 //'version' => $row['Version'],
1128 'state' => substr($state,3),
1129 'motd' => $row['MOTD'],
1135 else
1137 nt_common_add_debug('in tool_main_get_shards_info_from_db() : shard_list2 is empty !');
1138 //nt_common_add_debug('dumping : filters');
1139 //nt_common_add_debug($filters);
1142 else
1144 // db error
1150 return $shard_list_result;
1153 function tool_main_get_su_from_status($status)
1155 if (is_array($status) && sizeof($status))
1157 reset($status);
1158 foreach($status as $sline)
1160 if ($sline['ShortName'] == 'SU')
1162 return $sline['AliasName'];
1167 return null;
1170 function tool_main_set_restart_mode($domain_id, $shard_id, $restart_mode=0)
1172 global $db, $nel_user, $tpl;
1173 global $AS_ShardRestart, $AS_ShardDomainRestart;
1175 $sequence_info = tool_main_get_restart_sequence($nel_user['user_name'], $domain_id, $shard_id);
1177 if (!$sequence_info)
1179 $sequence_info = tool_main_add_restart_sequence($nel_user['user_name'], $domain_id, $shard_id, $restart_mode);
1182 if ($sequence_info)
1184 $sql = "UPDATE ". NELDB_SHARD_TABLE ." SET shard_restart=". $sequence_info['restart_sequence_id'] ." WHERE shard_id=". $shard_id ." AND shard_domain_id=". $domain_id;
1185 $db->sql_query($sql);
1187 // update shards information
1188 $nel_user['access']['user_shards'] = tool_admin_users_shards_get_list($nel_user['user_id']);
1189 $nel_user['access']['group_shards'] = tool_admin_groups_shards_get_list($nel_user['user_group_id']);
1190 $nel_user['access']['shards'] = tool_admin_users_groups_shards_merge();
1191 $tpl->assign('tool_shard_list', $nel_user['access']['shards']);
1193 // update shard restart information
1194 $AS_ShardRestart = tool_main_get_shard_data($shard_id, 'shard_restart');
1195 $AS_ShardDomainRestart = tool_main_get_domain_shard_restart($view_domain_id);
1196 $tpl->assign('tool_shard_restart_status', $AS_ShardRestart);
1197 $tpl->assign('tool_domain_has_shard_restart', $AS_ShardDomainRestart);
1200 return $sequence_info;
1203 function tool_main_add_restart_sequence($user_name, $domain_id, $shard_id, $step=0)
1205 global $db;
1206 global $nel_user;
1207 global $AS_Name, $AS_ShardName;
1208 global $restart_notification_emails;
1210 $now = time();
1212 $sql = "INSERT INTO ". NELDB_RESTART_SEQUENCE_TABLE;
1213 $sql .= " (`restart_sequence_domain_id`,`restart_sequence_shard_id`,`restart_sequence_user_name`,";
1214 $sql .= " `restart_sequence_step`,`restart_sequence_date_start`,`restart_sequence_date_end`) VALUES ";
1215 $sql .= " (". $domain_id .",". $shard_id .",'". $user_name ."',";
1216 $sql .= $step .",". $now .",". $now .")";
1217 $db->sql_query($sql);
1219 $sequence_info = tool_main_get_restart_sequence_by_id($db->sql_nextid());
1221 nt_log("Shard Restart (Domain: '". $AS_Name ."' - Shard: '". $AS_ShardName ."' - Sequence: '". $sequence_info['restart_sequence_id'] ."') started by '". $nel_user['user_name'] ."'");
1223 $email_subject = "[Shard Admin Tool] Restart Sequence Begins (id: ".$sequence_info['restart_sequence_id'].", step: ". $step .") for shard ".$AS_Name."/".$AS_ShardName." by ".$nel_user['user_name'];
1224 $email_message = $email_subject;
1225 nt_email($email_subject,$email_message,$restart_notification_emails);
1227 return $sequence_info;
1230 function tool_main_get_restart_sequence($user_name, $domain_id, $shard_id)
1232 global $db;
1234 $data = null;
1236 $sql = "SELECT * FROM ". NELDB_RESTART_SEQUENCE_TABLE ." WHERE restart_sequence_domain_id=". $domain_id ." AND restart_sequence_shard_id=". $shard_id ." AND restart_sequence_user_name='". $user_name ."' AND restart_sequence_step=0 AND restart_sequence_date_start=restart_sequence_date_end";
1237 if ($result = $db->sql_query($sql))
1239 if ($db->sql_numrows($result))
1241 $data = $db->sql_fetchrow($result);
1245 return $data;
1248 function tool_main_get_restart_sequence_by_id($sequence_id)
1250 global $db;
1252 $data = null;
1254 $sql = "SELECT * FROM ". NELDB_RESTART_SEQUENCE_TABLE ." WHERE restart_sequence_id=". $sequence_id;
1255 if ($result = $db->sql_query($sql))
1257 if ($db->sql_numrows($result))
1259 $data = $db->sql_fetchrow($result);
1263 return $data;
1266 function tool_main_set_next_restart_sequence_step($sequence_id, $step=null)
1268 global $db;
1269 global $nel_user;
1270 global $AS_Name, $AS_ShardName;
1271 global $restart_notification_emails;
1273 $now = time();
1275 if ($step === null)
1277 $sequence_info = tool_main_get_restart_sequence_by_id($sequence_id);
1278 $step = $sequence_info['restart_sequence_step'] + 1;
1281 $sql = "UPDATE ". NELDB_RESTART_SEQUENCE_TABLE ." SET restart_sequence_step=". $step .",restart_sequence_date_end=". $now ." WHERE restart_sequence_id=". $sequence_id;
1282 $db->sql_query($sql);
1284 nt_log("Shard Restart (Domain: '". $AS_Name ."' - Shard: '". $AS_ShardName ."' - Sequence: '". $sequence_id ."') - ". $nel_user['user_name'] ." moved to step ". $step);
1286 if ($step !== null)
1288 $email_subject = "[Shard Admin Tool] Restart Sequence Updated (id: ".$sequence_id.", step: ". $step .") for shard ".$AS_Name."/".$AS_ShardName." by ".$nel_user['user_name'];
1289 $email_message = $email_subject;
1290 nt_email($email_subject,$email_message,$restart_notification_emails);
1293 return tool_main_get_restart_sequence_by_id($sequence_id);
1296 function tool_main_set_end_restart_sequence($sequence_id)
1298 global $db;
1299 global $nel_user;
1300 global $AS_Name, $AS_ShardName;
1301 global $restart_notification_emails;
1303 //$sequence_info = tool_main_set_next_restart_sequence_step($sequence_id);
1304 $sequence_info = tool_main_get_restart_sequence_by_id($sequence_id);
1306 $sql = "UPDATE ". NELDB_SHARD_TABLE ." SET shard_restart=0 WHERE shard_domain_id=". $sequence_info['restart_sequence_domain_id'] ." AND shard_id=". $sequence_info['restart_sequence_shard_id'];
1307 $db->sql_query($sql);
1309 nt_log("Shard Restart (Domain: '". $AS_Name ."' - Shard: '". $AS_ShardName ."' - Sequence: '". $sequence_id ."') - ". $nel_user['user_name'] ." ended the sequence !". $step);
1311 $email_subject = "[Shard Admin Tool] Restart Sequence Ends (id: ".$sequence_info['restart_sequence_id'].", step: ". $sequence_info['restart_sequence_step'] .") for shard ".$AS_Name."/".$AS_ShardName." by ".$nel_user['user_name'];
1312 $email_message = $email_subject;
1313 nt_email($email_subject,$email_message,$restart_notification_emails);
1316 function tool_main_get_domain_shard_restart($domain_id)
1318 global $db;
1320 $num = 0;
1321 $sql = "SELECT * FROM ". NELDB_SHARD_TABLE ." WHERE shard_domain_id=". $domain_id ." AND shard_restart>0";
1322 if ($result = $db->sql_query($sql))
1324 $num = $db->sql_numrows($result);
1327 return $num;
1330 function tool_main_get_restart_services($InternalName, $domainServices, $restart_list)
1332 // lets sort each services by 'type'
1333 nt_common_add_debug("RESTART DEBUG");
1334 nt_common_add_debug($domainServices);
1336 $sortedServices = array();
1338 reset($domainServices);
1339 foreach($domainServices as $domainService)
1341 if ($domainService['ShardName'] == $InternalName)
1343 // this should be the real 'type'
1344 // $type = $domainService['ShortName'];
1345 // but instead we'll take the first part of the AliasName (as in "rws" or "rws_aniro")
1346 $type = explode('_',$domainService['AliasName']);
1347 $type = trim(strtolower($type[0]));
1349 if (!isset($sortedServices[$type]))
1351 $sortedServices[$type] = array();
1354 $sortedServices[$type][] = $domainService['AliasName'];
1355 nt_common_add_debug("Adding '". $domainService['AliasName'] ."' to '$type'");
1359 reset($restart_list);
1360 foreach($restart_list as $restart_key => $restart_group)
1362 $group_list = explode(',', $restart_group['restart_group_list']);
1363 $tmp_list = array();
1365 reset($group_list);
1366 foreach($group_list as $group_service_type)
1368 $group_service_type = trim($group_service_type);
1369 nt_common_add_debug("looking for '$group_service_type'");
1371 if (isset($sortedServices[$group_service_type]))
1373 $tmp_list[] = implode(',',$sortedServices[$group_service_type]);
1375 else
1377 nt_common_add_debug("not found!");
1381 $restart_list[$restart_key]['service_list'] = implode(',',$tmp_list);
1385 nt_common_add_debug("RESTART SERVICES DEBUG");
1386 nt_common_add_debug($restart_list);
1388 return $restart_list;
1391 function tool_main_get_all_restart_services($restart_list)
1393 $stop_data = array();
1394 $stop_list = '';
1396 reset($restart_list);
1397 foreach($restart_list as $restart_key => $restart_group)
1399 $stop_data[] = $restart_group['service_list'];
1402 $stop_list = implode(',', $stop_data);
1403 return $stop_list;
1406 function tool_main_get_shardid_from_status($domainServices, $InternalName)
1408 reset($domainServices);
1409 foreach($domainServices as $service)
1411 if (($service['ShardName'] == $InternalName) && (is_numeric($service['ShardId'])))
1413 return $service['ShardId'];
1417 return null;
1420 function tool_main_get_egs_from_status($domainServices, $InternalName)
1422 reset($domainServices);
1423 foreach ($domainServices as $service)
1425 if (($service['ShardName'] == $InternalName) && ($service['ShortName'] == 'EGS'))
1427 return $service['AliasName'];
1431 return null;
1434 function tool_main_set_restart_sequence_timer($sequence_id, $timer)
1436 global $db;
1438 $now = time();
1439 $timer = $timer;
1440 $new_timer = $now + $timer;
1442 $sql = "UPDATE ". NELDB_RESTART_SEQUENCE_TABLE ." SET restart_sequence_date_end=". $now .",restart_sequence_timer=". $new_timer ." WHERE restart_sequence_id=". $sequence_id;
1443 $db->sql_query($sql);
1445 return array('start' => $now, 'end' => $new_timer);
1448 function tool_main_change_restart_notification($event_list, $mode, $sql_connection)
1450 if ($sql_connection != '' && sizeof($event_list))
1452 $csdb = new sql_db_string($sql_connection);
1453 if (is_object($csdb))
1455 $sql = "UPDATE Sorbot_events SET event_action=". $mode .",event_time=". time() .",event_lap=0 WHERE event_id IN (". implode(',', array_values($event_list)) .")";
1456 $csdb->sql_query($sql);
1458 $csdb->sql_close();
1463 function tool_main_add_restart_notification($sequence_id, $service_su, $service_egs, $shard_id, $sorbot_message_type, $sql_connection, $shard_lang='en')
1465 nt_common_add_debug('tool_main_add_restart_notification()');
1467 if ($sql_connection != '')
1469 $csdb = new sql_db_string($sql_connection);
1470 if (is_object($csdb))
1472 $sequence_info = tool_main_get_restart_sequence_by_id($sequence_id);
1473 $shard_info = tool_main_get_domain_shard_data($sequence_info['restart_sequence_domain_id'], $sequence_info['restart_sequence_shard_id']);
1475 if ($sorbot_message_type == 4102)
1477 //$open_timer = 60*25; // 25 minutes before a shard opens itself
1478 // new timer
1479 $open_timer = 60*10; // 10 minutes before a shard opens itself
1481 else // 4101
1483 $open_timer = 0;
1486 if ($sequence_info && $shard_info)
1488 // lets find the shard id used by the ticket system
1489 $sql = "SELECT * FROM ForumCS_tickets_shards WHERE shard_ca='". $shard_info['domain_application'] ."' AND shard_id='". $shard_id ."'";
1490 if ($result = $csdb->sql_query($sql))
1492 if ($csdb->sql_numrows($result))
1494 $ticketsystem_shard_info = $csdb->sql_fetchrow($result);
1495 nt_common_add_debug($ticketsystem_shard_info);
1497 $ticketsystem_shard_id = $ticketsystem_shard_info['id'];
1499 // now we have the shard id, lets see which klients servers wants events for it
1500 $sql = "SELECT * FROM Sorbot_botconfig WHERE config_name='shardRestart' AND config_value LIKE '%:". $ticketsystem_shard_id .":%'";
1501 if ($result2 = $csdb->sql_query($sql))
1503 if ($csdb->sql_numrows($result2))
1505 //$klients_servers = array();
1506 //while ($row = $csdb->sql_fetchrow($result2))
1508 // $klients_servers[] = $row['server_name'];
1511 // NOTE: live but not in CVS yet !
1512 $local_timer = $open_timer;
1514 $klients_servers = array();
1515 while ($row = $csdb->sql_fetchrow($result2))
1517 $klients_servers[] = $row['server_name'];
1519 // lets find if there is any specific opening timer, and keep the lowest one
1520 $sql = "SELECT * FROM Sorbot_botconfig WHERE server_name='". $row['server_name'] ."' AND config_name='shardRestartOpenTimer'";
1521 if ($result3 = $csdb->sql_query($sql))
1523 if ($csdb->sql_numrows($result3))
1525 $timer_row = $csdb->sql_fetchrow($result3);
1526 if ($timer_row['config_value'] < $local_timer)
1528 $local_timer = $timer_row['config_value'];
1534 $open_timer = $local_timer;
1535 // END NOTE: live but not in CVS yet !
1537 // ok now we the list of servers that want this event, lets give it to them
1539 // lets build some messages
1540 $klients_countdown = '';
1541 tool_main_get_elapsed_time_string($sequence_info['restart_sequence_timer'] - $sequence_info['restart_sequence_date_end'], $klients_countdown);
1542 $klients_countdown = trim($klients_countdown);
1544 $klients_open_countdown = '';
1545 tool_main_get_elapsed_time_string($open_timer, $klients_open_countdown);
1546 $klients_open_countdown = trim($klients_open_countdown);
1548 $opening_message = tool_admin_restart_messages_get_list_from_name('opening',$shard_lang);
1549 $opening_message = $opening_message[0]['restart_message_value'];
1551 $opened_message = tool_admin_restart_messages_get_list_from_name('opened',$shard_lang);
1552 $opened_message = $opening_message[0]['restart_message_value'];
1554 $klients_messages = array( 'before' => array(
1555 ".topic Shard Maintenance in progress for '". $ticketsystem_shard_info['shard_name'] ."' - Shard LOCKED - (". date("r",$sequence_info['restart_sequence_date_start']) ." - Countdown set to ". $klients_countdown .")",
1556 "[Shard Maintenance] A Shard Restart has been triggered for shard '". $ticketsystem_shard_info['shard_name'] ."' on ". date("r",$sequence_info['restart_sequence_date_start']),
1557 "[Shard Maintenance] Shard is LOCKED and should go down in ". $klients_countdown,
1559 'at' => array(
1560 ".topic Shard Maintenance in progress for '". $ticketsystem_shard_info['shard_name'] ."' - Shard DOWN - (". date("r",$sequence_info['restart_sequence_date_start']) .")",
1561 "[Shard Maintenance] A Shard Restart has been triggered for shard '". $ticketsystem_shard_info['shard_name'] ."' on ". date("r",$sequence_info['restart_sequence_date_start']),
1562 "[Shard Maintenance] Shard is DOWN after a countdown of ". $klients_countdown,
1564 'over' => array(
1565 ".topic Shard Maintenance in finished for '". $ticketsystem_shard_info['shard_name'] ."' - Shard LOCKED - Ready for CSRs ! ETA : |EventETA| - To Open : .sorOpenShard |EventID| 0",
1566 "[Shard Maintenance] The Shard Restart that has been triggered for shard '". $ticketsystem_shard_info['shard_name'] ."' on ". date("r",$sequence_info['restart_sequence_date_start']) ." is now finished !",
1567 "[Shard Maintenance] Shard is LOCKED and Ready for CSRs, it will OPEN automatically in |EventTIME| !",/*. $klients_open_countdown ." !",*/
1568 "[Shard Maintenance] To change the OPEN countdown, you need to send me the command : .sorOpenShard |EventID| <seconds>",
1569 "[Shard Maintenance] Example to OPEN the Shard now, you need to send me the command : .sorOpenShard |EventID| 0",
1571 'open' => array(
1572 ".topic Shard Maintenance over for '". $ticketsystem_shard_info['shard_name'] ."' - Shard OPEN ",
1573 "[Shard Maintenance] The shard '". $ticketsystem_shard_info['shard_name'] ."' should now be OPEN to all !",
1575 'cancel' => array(
1576 ".topic Shard Maintenance has been cancelled for '". $ticketsystem_shard_info['shard_name'] ."' - Shard OPEN",
1577 "[Shard Maintenance] The shard restart has been CANCELLED for '". $ticketsystem_shard_info['shard_name'] ."', the shard is OPEN again !",
1579 'giveup' => array(
1580 ".topic Shard Maintenance for '". $ticketsystem_shard_info['shard_name'] ."' is on hold for technical issues - Shard DOWN",
1581 "[Shard Maintenance] The shard restart has been set ON HOLD for technical issues !",
1583 'opening' => $opening_message,
1584 'opened' => $opened_message,
1587 $notification_info = array( 'sequence' => $sequence_info,
1588 'ts' => $ticketsystem_shard_info,
1589 'messages' => $klients_messages,
1590 'shard' => $shard_info,
1591 'opentimer' => $open_timer,
1592 'shardid' => $shard_id,
1593 'shardsu' => $service_su,
1594 'shardegs' => $service_egs,
1597 $notification_data = base64_encode(serialize($notification_info));
1599 $event_insert_ids = array();
1601 $sql = "LOCK TABLES Sorbot_events WRITE";
1602 $csdb->sql_query($sql);
1604 reset($klients_servers);
1605 foreach($klients_servers as $klient_server)
1607 $sql = "INSERT INTO Sorbot_events (`server_name`,`event_action`,`event_time`,`event_lap`,`event_params`) ";
1608 $sql .= " VALUES ('". $klient_server ."',". $sorbot_message_type .",". time() .",". $open_timer .",'". $notification_data ."')";
1609 $csdb->sql_query($sql);
1610 $event_insert_ids[] = $csdb->sql_nextid();
1613 // we have a list of all the events, lets save them somewhere
1614 // lets inform each events of all other events for the same sequence
1615 if (sizeof($event_insert_ids))
1617 $notification_info['eventlist'] = array_values($event_insert_ids);
1618 $notification_data = base64_encode(serialize($notification_info));
1620 $sql = "UPDATE Sorbot_events SET event_params='". $notification_data ."' WHERE event_id IN (". implode(',', array_values($event_insert_ids)) .")";
1621 $csdb->sql_query($sql);
1623 // save the events locally too
1624 tool_main_set_restart_sequence_events($sequence_id, implode(',',array_values($event_insert_ids)));
1627 $sql = "UNLOCK TABLES";
1628 $csdb->sql_query($sql);
1630 else
1632 nt_common_add_debug('no klients server found wanting to hear about this event');
1636 else
1638 nt_common_add_debug('no shard found in TS matching the application and shard id');
1641 else
1643 nt_common_add_debug('query failed ?!');
1646 else
1648 nt_common_add_debug('missing some information about the sequence ?!');
1651 $csdb->sql_close();
1653 else
1655 nt_common_add_debug('failed to init db!');
1658 else
1660 nt_common_add_debug('no db string to work with');
1664 function tool_main_set_restart_sequence_events($sequence_id, $events)
1666 global $db;
1668 $sql = "UPDATE ". NELDB_RESTART_SEQUENCE_TABLE ." SET restart_sequence_events='". $events ."' WHERE restart_sequence_id=". $sequence_id;
1669 $db->sql_query($sql);
1672 function tool_main_get_domain_shard_data($domain_id, $shard_id)
1674 global $db;
1676 $data = null;
1678 $sql = "SELECT ". NELDB_DOMAIN_TABLE .".domain_id,";
1679 $sql .= NELDB_DOMAIN_TABLE .".domain_name,";
1680 $sql .= NELDB_DOMAIN_TABLE .".domain_as_host,";
1681 $sql .= NELDB_DOMAIN_TABLE .".domain_as_port,";
1682 $sql .= NELDB_DOMAIN_TABLE .".domain_application,";
1683 $sql .= NELDB_SHARD_TABLE .".shard_id,";
1684 $sql .= NELDB_SHARD_TABLE .".shard_name,";
1685 $sql .= NELDB_SHARD_TABLE .".shard_as_id,";
1686 $sql .= NELDB_SHARD_TABLE .".shard_domain_id,";
1687 $sql .= NELDB_SHARD_TABLE .".shard_lang,";
1688 $sql .= NELDB_SHARD_TABLE .".shard_restart";
1689 $sql .= " FROM ". NELDB_DOMAIN_TABLE .",". NELDB_SHARD_TABLE;
1690 $sql .= " WHERE domain_id=shard_domain_id AND domain_id=". $domain_id ." AND shard_id=". $shard_id;
1692 if ($result = $db->sql_query($sql))
1694 if ($db->sql_numrows($result))
1696 $data = $db->sql_fetchrow($result);
1700 return $data;
1703 function tool_main_set_restart_sequence_user($AS_ShardRestart)
1705 global $db;
1706 global $AS_Name, $AS_ShardName;
1707 global $nel_user;
1708 global $restart_notification_emails;
1710 $sequence_info = tool_main_get_restart_sequence_by_id($AS_ShardRestart);
1712 if ($sequence_info)
1714 $sql = "UPDATE ". NELDB_RESTART_SEQUENCE_TABLE ." SET restart_sequence_user_name='". $nel_user['user_name'] ."' WHERE restart_sequence_id=". $AS_ShardRestart;
1715 $db->sql_query($sql);
1717 nt_log("Shard Restart (Domain: '". $AS_Name ."' - Shard: '". $AS_ShardName ."' - Sequence: '". $AS_ShardRestart ."') owner set to '". $nel_user['user_name'] ."'");
1719 $email_subject = "[Shard Admin Tool] Restart Sequence Takeover (id: ".$sequence_info['restart_sequence_id'].", step: ". $sequence_info['restart_sequence_step'] .") for shard ".$AS_Name."/".$AS_ShardName." by ".$nel_user['user_name'];
1720 $email_message = $email_subject;
1721 nt_email($email_subject,$email_message,$restart_notification_emails);