";
535 | print foot();
536 | exit(0);
537 | }
538 |
539 | function get_request_params($data =""){
540 | $ret = array();
541 | $err = array();
542 |
543 | if (!$data){
544 | $err[]=sprintf("Параметры запроса отсутствуют%s",is_developer() ? sprintf(" (file: %s, func: %s, line: %s)",__FILE__,__FUNCTION__,__LINE__) : "");
545 | }
546 |
547 | if (count($err) == 0){
548 | $decode=base64_decode($data);
549 | if ($decode === false){
550 | $err[]=sprintf("Неверные параметры запроса%s",is_developer() ? sprintf(" (file: %s, func: %s, line: %s)",__FILE__,__FUNCTION__,__LINE__) : "");
551 | }
552 | }
553 |
554 | if (count($err) == 0){
555 | $expl = explode("&",$decode);
556 | foreach ($expl as $str){
557 | $tmp = explode("=",$str);
558 | if (count($tmp) == 2){
559 | if (!isset($params)){
560 | $params = array();
561 | }
562 | $params[$tmp[0]]=$tmp[1];
563 | }
564 | }
565 |
566 | if (!isset($params) || !is_array($params)){
567 | $err[]=sprintf("Параметры запроса пусты%s",is_developer() ? sprintf(" (file: %s, func: %s, line: %s)",__FILE__,__FUNCTION__,__LINE__) : "");
568 | }
569 | }
570 |
571 | if (count($err) == 0){
572 | $ret['data']=$params;
573 | }
574 |
575 | if (count($err) > 0){
576 | $ret['error'] = $err;
577 | }
578 |
579 | return $ret;
580 | }
581 |
582 | function chk_auth( $fatal = 0){
583 | if (isset($_SESSION['auth']) && $_SESSION['auth'] == 1){
584 | return 1;
585 | }else{
586 | if ($fatal){
587 | @session_destroy();
588 | print error("Вы не авторизованы");
589 | printf("\n\n",URL);
590 | exit(0);
591 | }
592 | }
593 | return 0;
594 | }
595 |
596 | function goback( $request, $funcParam =array() ){
597 | $update = "";
598 | $ret = "";
599 | $location = "index.php";
600 |
601 | if (isset($request['location'])){
602 | $location = $request['location'];
603 | }
604 | unset($request['data'],$request['location']);
605 |
606 | $timeout=3000;
607 | if (isset($funcParam['timeout']) && preg_match("/^\d+$/",$funcParam['timeout']) && (int)$funcParam['timeout'] > 0){
608 | $timeout=$funcParam['timeout'];
609 | if ($timeout < 1000){
610 | $timeout=$timeout*1000;
611 | }
612 | }
613 |
614 | $ret.="\n";
631 | return $ret;
632 | }
633 |
634 | function menu(){
635 | $ret = array();
636 | if (isset($_SESSION['auth']) && $_SESSION['auth'] == 1){
637 | $ret = array(
638 | 'exit' => array(
639 | 'location' => 'plugins/logout.php',
640 | 'class' => 'exit',
641 | 'text' => 'Выход',
642 | )
643 | );
644 | }
645 | return $ret;
646 | }
647 |
648 | function get_hosts(){
649 | $ret = array();
650 | $err = array();
651 |
652 | $list=read_dir(
653 | array(
654 | "dir"=>COLLECTOR_DATA_DIR,
655 | "filter"=>"^host_",
656 | )
657 | );
658 |
659 | if (isset($list['error'])){
660 | $err=$list['error'];
661 | }
662 |
663 | if (count($err) == 0){
664 | if (!isset($list['list']) || !is_array($list['list']) || count($list['list']) == 0){
665 | $err[]=sprintf("Хостов не найдено%s",is_developer() ? sprintf(" (file: %s, func: %s, line: %s)",__FILE__,__FUNCTION__,__LINE__) : "");
666 | }
667 | }
668 |
669 | if (count($err) == 0){
670 | $tmp=array();
671 | $nn=1;
672 | foreach ($list['list'] as $k=>$f){
673 | if (preg_match("/^host_(\S+)_(\S+)\.json$/",$f,$m)){
674 | $group=get_group($m[1]);
675 | $key = isset($tmp[$m[1]]) ? sprintf("%s_%d",$m[1],$nn) : $m[1];
676 | $tmp[$group][$key]=array(
677 | 'monit_id' => $m[2],
678 | 'name' => $m[1],
679 | 'file' => $f,
680 | 'group' => $group,
681 | );
682 | $rf=read_host_file(array("file"=>sprintf("%s/%s/%s",LOC,COLLECTOR_DATA_DIR,$f)));
683 | if (isset($rf['error'])){
684 | $tmp[$group][$key]['error']=$rf['error'];
685 | }else{
686 | $tmp[$group][$key]['data']=$rf['data'];
687 | }
688 | $nn++;
689 | }
690 | }
691 | ksort($tmp);
692 | foreach ($tmp as $k=>$v){
693 | ksort($tmp[$k]);
694 | }
695 | foreach ($tmp as $k=>$v){
696 | foreach ($v as $hn=>$hv){
697 | $ret['hosts'][$k][$hv['monit_id']]=$hv;
698 | }
699 | }
700 | if (count($ret['hosts']) == 0){
701 | $err[]=sprintf("Хосты отсутствуют%s",is_developer() ? sprintf(" (file: %s, func: %s, line: %s)",__FILE__,__FUNCTION__,__LINE__) : "");
702 | }
703 | }
704 |
705 | if (count($err) > 0){
706 | $ret['error']=$err;
707 | }
708 | return $ret;
709 | }
710 |
711 | function get_group( $name = ""){
712 | $ret="z_noname_group";
713 | if ($name){
714 | $p=parse_url($name);
715 | if (isset($p['path']) && $p['path']){
716 | $tmp=explode(".",$p['path']);
717 | if (count($tmp) > 1){
718 | if (count($tmp) == 2){
719 | $ret=sprintf("%s.%s",$tmp[0],$tmp[1]);
720 | }else{
721 | $ret=sprintf("%s.%s",$tmp[count($tmp)-2],$tmp[count($tmp)-1]);
722 | }
723 | }
724 | }
725 | }
726 | return $ret;
727 | }
728 |
729 | function parse_services( $data = array() ){
730 | global $services,$sysUsers,$sysGroups;
731 |
732 | $ret=array();
733 | foreach ($services as $k=>$v){
734 | $type[$v['key']] = array();
735 | }
736 | $ret['alarm'] = 0;
737 | //Хост без сервисов
738 | if (isset($data['service']['@attributes'])){
739 | $tmp=$data['service'];
740 | $data['service']=array($tmp);
741 | unset($tmp);
742 | }
743 | //
744 | foreach($data as $key=>$srv){
745 | if (is_array($srv)){
746 | foreach ($srv as $k=>$v){
747 | if (isset($v['type'])){
748 | $status_2_header = "";
749 | $key = isset($services[$v['type']]['key']) ? $services[$v['type']]['key'] : $services[-1]['key'];
750 | if (isset($v['status'])){
751 | if ($v['status'] > 0){
752 | $ret['alarm']++;
753 | $status_2_header=sprintf(" ::
статус %d",$v['status']);
754 | }
755 | }
756 | if (!isset($v['monitor'])){
757 | $v['monitor'] = 0;
758 | }
759 | if ($v['monitor'] == 0){
760 | $type[$key][]=sprintf("
%s :: Не мониторится
",$v['@attributes']['name']);
761 | }elseif ($v['monitor'] == 2){
762 | $type[$key][]=sprintf("
%s :: Идет сбор данных
",$v['@attributes']['name']);
763 | }
764 | if ($v['type'] == 5 && $v['monitor'] == 1){
765 | //system
766 | $type[$key][]=sprintf("
%s%s
",$v['@attributes']['name'],$status_2_header);
767 | if (isset($v['system']['load']) && isset($v['system']['cpu']['user']) && isset($v['system']['cpu']['system'])){
768 | $type[$key][]="
";
769 | $type[$key][]="";
770 | $type[$key][]="";
771 | $type[$key][]="| Load | ";
772 | $type[$key][]="CPU | ";
773 | $type[$key][]="Memory | ";
774 | $type[$key][]="Swap | ";
775 | $type[$key][]="
";
776 | $type[$key][]="";
777 | $type[$key][]=sprintf("",$v['status'] > 0 ? " alarm" : "");
778 | $type[$key][]=sprintf("| [%s] | ",implode("] [",$v['system']['load']));
779 | $type[$key][]=sprintf("%s%%us,%s%%sy | ",$v['system']['cpu']['user'],$v['system']['cpu']['system']);
780 | $type[$key][]=sprintf("%s%s | ",isset($v['system']['memory']['percent']) ? sprintf("%s%%",$v['system']['memory']['percent']) : "n/a",(isset($v['system']['memory']['kilobyte']) && $v['system']['memory']['kilobyte'] > 0) ? sprintf(" [%s]",get_bwk($v['system']['memory']['kilobyte']*1024)) : "");
781 | $type[$key][]=sprintf("%s%s | ",isset($v['system']['swap']['percent']) ? sprintf("%s%%",$v['system']['swap']['percent']) : "n/a",(isset($v['system']['swap']['kilobyte']) && $v['system']['swap']['kilobyte'] > 0) ? sprintf(" [%s]",get_bwk($v['system']['swap']['kilobyte']*1024)) : "");
782 | $type[$key][]="
";
783 | $type[$key][]="
";
784 | }else{
785 | $type[$key][]="Нет данных";
786 | }
787 | }elseif ($v['type'] == 3 && $v['monitor'] == 1){
788 | //process
789 | $type[$key][]=sprintf("
%s%s
",$v['@attributes']['name'],$status_2_header);
790 | if (isset($v['cpu']) && isset($v['memory']) && isset($v['uptime'])){
791 | $type[$key][]="
";
792 | $type[$key][]="";
793 | $type[$key][]="";
794 | $type[$key][]="| CPU | ";
795 | $type[$key][]="Memory | ";
796 | $type[$key][]="uptime | ";
797 | $type[$key][]="PID | ";
798 | $type[$key][]="UID | ";
799 | $type[$key][]="GID | ";
800 | $type[$key][]="threads | ";
801 | $type[$key][]="children | ";
802 | $type[$key][]="
";
803 | $type[$key][]="";
804 | $type[$key][]=sprintf("",$v['status'] > 0 ? " alarm" : "");
805 | $type[$key][]=sprintf("| %s%% | ",$v['cpu']['percenttotal'] > 0 ? $v['cpu']['percenttotal'] : 0);
806 | $type[$key][]=sprintf("%s%%%s | ",$v['memory']['percenttotal'] > 0 ? $v['memory']['percenttotal'] : 0,(isset($v['memory']['kilobytetotal']) && $v['memory']['kilobytetotal'] > 0) ? sprintf(" [%s]",get_bwk($v['memory']['kilobytetotal']*1024)) : "");
807 | $type[$key][]=sprintf("%s | ",uptime($v['uptime'],"string"));
808 | $type[$key][]=sprintf("%s | ",$v['pid']);
809 | $type[$key][]=sprintf("%s | ",$v['uid']);
810 | $type[$key][]=sprintf("%s | ",$v['gid']);
811 | $type[$key][]=sprintf("%s | ",$v['threads']);
812 | $type[$key][]=sprintf("%s | ",$v['children']);
813 | $type[$key][]="
";
814 | $type[$key][]="
";
815 |
816 | if (isset($v['port'])){
817 | $type[$key][]="
";
818 | $type[$key][]="";
819 | $type[$key][]="";
820 | $type[$key][]="| Тип | ";
821 | $type[$key][]="Хост | ";
822 | $type[$key][]="Порт | ";
823 | $type[$key][]="Протокол | ";
824 | $type[$key][]="Время ответа | ";
825 | $type[$key][]="
";
826 | $type[$key][]="";
827 | if (!isset($v['port'][0])){
828 | $ptmp=$v['port'];
829 | $v['port']=array( '0' => $ptmp );
830 | unset($ptmp);
831 | }
832 | foreach ($v['port'] as $kp=>$pv){
833 | $class = "";
834 | if ($v['status'] > 0){
835 | $class = " alarm";
836 | if (isset($pv['responsetime']) && $pv['responsetime'] > 0 ){
837 | $class = "";
838 | }
839 | }
840 | $type[$key][]=sprintf("",$class);
841 | $type[$key][]="| Порт | ";
842 | $type[$key][]=sprintf("%s | ",isset($pv['hostname']) ? $pv['hostname'] : "n/a");
843 | $type[$key][]=sprintf("%s | ",isset($pv['portnumber']) ? $pv['portnumber'] : "n/a");
844 | $type[$key][]=sprintf("%s%s | ",isset($pv['type']) ? $pv['type'] : "",isset($pv['protocol']) ? sprintf(" (%s)",$pv['protocol']) : "");
845 | $type[$key][]=sprintf("%s | ",(isset($pv['responsetime']) && $pv['responsetime'] > 0 ) ? $pv['responsetime'] : "n/a");
846 | $type[$key][]="
";
847 | }
848 | unset($class);
849 | $type[$key][]="
";
850 | }
851 | }else{
852 | $type[$key][]="Нет данных";
853 | }
854 | }elseif ($v['type'] == 8 && $v['monitor'] == 1){
855 | //Network
856 | $type[$key][]=sprintf("
%s%s
",$v['@attributes']['name'],$status_2_header);
857 | $type[$key][]="
";
858 | $type[$key][]="";
859 | $type[$key][]="";
860 | $type[$key][]="| Линк | ";
861 | $type[$key][]="Скорость | ";
862 | $type[$key][]="Download | ";
863 | $type[$key][]="Upload | ";
864 | $type[$key][]="
";
865 | $type[$key][]="";
866 | $type[$key][]="| Пакеты | ";
867 | $type[$key][]="Передано | ";
868 | $type[$key][]="Ошибки | ";
869 | $type[$key][]="Пакеты | ";
870 | $type[$key][]="Передано | ";
871 | $type[$key][]="Ошибки | ";
872 | $type[$key][]="
";
873 | $type[$key][]="";
874 | $type[$key][]=sprintf("",$v['status'] > 0 ? " alarm" : "");
875 | $type[$key][]=sprintf("| %s | ",$v['link']['state'] == 1 ? "Да" : "Нет");
876 | if ($v['link']['state'] == 1){
877 | $type[$key][]=sprintf("%sMbit %s | ",$v['link']['speed']/1000000,$v['link']['duplex'] == 1 ? "full-duplex" : "half-duplex");
878 | $type[$key][]=sprintf("%s | ",$v['link']['download']['packets']['total']);
879 | $type[$key][]=sprintf("%s | ",get_bwk($v['link']['download']['bytes']['total']));
880 | $type[$key][]=sprintf("%s | ",$v['link']['download']['errors']['total'] > 0 ? " class=warn" : "",$v['link']['download']['errors']['total']);
881 | $type[$key][]=sprintf("%s | ",$v['link']['upload']['packets']['total']);
882 | $type[$key][]=sprintf("%s | ",get_bwk($v['link']['upload']['bytes']['total']));
883 | $type[$key][]=sprintf("%s | ",$v['link']['upload']['errors']['total'] > 0 ? " class=warn" : "",$v['link']['upload']['errors']['total']);
884 | }else{
885 | $type[$key][]="n/a | ";
886 | }
887 | $type[$key][]="
";
888 | $type[$key][]="
";
889 |
890 | if (defined('NETWORK_ERRORS_AS_ALARM') && NETWORK_ERRORS_AS_ALARM === true){
891 | if ($v['link']['upload']['errors']['total'] > 0 || $v['link']['download']['errors']['total'] > 0){
892 | $ret['alarm']++;
893 | }
894 | }
895 | }elseif ($v['type'] == 0 && $v['monitor'] == 1){
896 | //Filesystem
897 | $type[$key][]=sprintf("
%s%s
",$v['@attributes']['name'],$status_2_header);
898 | if ($v['status'] == 512){
899 | $type[$key][]="
Раздела не существует";
900 | }else{
901 | $type[$key][]="
";
902 | $type[$key][]="";
903 | $type[$key][]="";
904 | $type[$key][]="| Тип | ";
905 | $type[$key][]="Флаги | ";
906 | $type[$key][]="Использовано | ";
907 | $type[$key][]="Использовано inodes | ";
908 | $type[$key][]="
";
909 | $type[$key][]="";
910 | $type[$key][]=sprintf("",$v['status'] > 0 ? " alarm" : "");
911 | $type[$key][]=sprintf("| %s | ",isset($v['fstype']) ? $v['fstype'] : "n/a");
912 | $type[$key][]=sprintf("%s | ",isset($v['fsflags']) ? (is_array($v['fsflags']) ? implode(", ",$v['fsflags']) : ($v['fsflags'] ? $v['fsflags'] : "нет") ) : "n/a");
913 | $type[$key][]=sprintf("%s%s | ",isset($v['block']['percent']) ? sprintf("%s%%",$v['block']['percent']) : "n/a",(isset($v['block']['usage']) && $v['block']['usage']> 0) ? sprintf(" [%s]",get_bwk($v['block']['usage']*1024*1024)) : "");
914 | $type[$key][]=sprintf("%s%s | ",isset($v['inode']['percent']) ? sprintf("%s%%",$v['inode']['percent']) : "n/a",(isset($v['inode']['usage']) && $v['inode']['usage'] > 0) ? sprintf(" [%s objects]",$v['inode']['usage']) : "");
915 | $type[$key][]="
";
916 | $type[$key][]="
";
917 | }
918 | }elseif ($v['type'] == 2 && $v['monitor'] == 1){
919 | //File checks
920 | $type[$key][]=sprintf("
%s%s
",$v['@attributes']['name'],$status_2_header);
921 | if ($v['status'] == 512){
922 | $type[$key][]="
Файла не существует";
923 | }else{
924 | $type[$key][]="
";
925 | $type[$key][]="";
926 | $type[$key][]="";
927 | $type[$key][]="| UID | ";
928 | $type[$key][]="GID | ";
929 | $type[$key][]="Mode | ";
930 | $type[$key][]="Размер | ";
931 | $type[$key][]="Checksum | ";
932 | $type[$key][]="Даты | ";
933 | $type[$key][]="
";
934 | $type[$key][]="";
935 | $type[$key][]="| access | ";
936 | $type[$key][]="change | ";
937 | $type[$key][]="modify | ";
938 | $type[$key][]="
";
939 | $type[$key][]="";
940 | $type[$key][]=sprintf("",$v['status'] > 0 ? " alarm" : "");
941 | $type[$key][]=sprintf("| %s | ",isset($v['uid']) ? (isset($sysUsers[$v['uid']]) ? $sysUsers[$v['uid']] : $v['uid']) : "n/a");
942 | $type[$key][]=sprintf("%s | ",isset($v['gid']) ? (isset($sysGroups[$v['gid']]) ? $sysGroups[$v['gid']] : $v['gid']) : "n/a");
943 | $type[$key][]=sprintf("%s | ",isset($v['mode']) ? $v['mode'] : "n/a");
944 | $type[$key][]=sprintf("%s | ",(isset($v['size']) && $v['size'] > 0) ? $v['size'] : "n/a");
945 | $type[$key][]=sprintf("%s | ",(isset($v['checksum']) && $v['checksum'] > 0) ? $v['checksum'] : "n/a");
946 | $type[$key][]=sprintf("%s | ",(isset($v['timestamps']['access']) && $v['timestamps']['access'] > 0) ? date("d.m.Y H:i:s",$v['timestamps']['access']) : "n/a");
947 | $type[$key][]=sprintf("%s | ",(isset($v['timestamps']['change']) && $v['timestamps']['change'] > 0) ? date("d.m.Y H:i:s",$v['timestamps']['change']) : "n/a");
948 | $type[$key][]=sprintf("%s | ",(isset($v['timestamps']['modify']) && $v['timestamps']['modify'] > 0) ? date("d.m.Y H:i:s",$v['timestamps']['modify']) : "n/a");
949 | $type[$key][]="
";
950 | $type[$key][]="
";
951 | }
952 | }elseif ($v['type'] == 1 && $v['monitor'] == 1){
953 | //Directory checks
954 | $type[$key][]=sprintf("
%s%s
",$v['@attributes']['name'],$status_2_header);
955 | if ($v['status'] == 512){
956 | $type[$key][]="
Директории не существует";
957 | }else{
958 | $type[$key][]="
";
959 | $type[$key][]="";
960 | $type[$key][]="";
961 | $type[$key][]="| UID | ";
962 | $type[$key][]="GID | ";
963 | $type[$key][]="Mode | ";
964 | $type[$key][]="Даты | ";
965 | $type[$key][]="
";
966 | $type[$key][]="";
967 | $type[$key][]="| access | ";
968 | $type[$key][]="change | ";
969 | $type[$key][]="modify | ";
970 | $type[$key][]="
";
971 | $type[$key][]="";
972 | $type[$key][]=sprintf("",$v['status'] > 0 ? " alarm" : "");
973 | $type[$key][]=sprintf("| %s | ",isset($v['uid']) ? (isset($sysUsers[$v['uid']]) ? $sysUsers[$v['uid']] : $v['uid']) : "n/a");
974 | $type[$key][]=sprintf("%s | ",isset($v['gid']) ? (isset($sysGroups[$v['gid']]) ? $sysGroups[$v['gid']] : $v['gid']) : "n/a");
975 | $type[$key][]=sprintf("%s | ",isset($v['mode']) ? $v['mode'] : "n/a");
976 | $type[$key][]=sprintf("%s | ",isset($v['timestamps']['access']) ? date("d.m.Y H:i:s",$v['timestamps']['access']) : "n/a");
977 | $type[$key][]=sprintf("%s | ",isset($v['timestamps']['change']) ? date("d.m.Y H:i:s",$v['timestamps']['change']) : "n/a");
978 | $type[$key][]=sprintf("%s | ",isset($v['timestamps']['modify']) ? date("d.m.Y H:i:s",$v['timestamps']['modify']) : "n/a");
979 | $type[$key][]="
";
980 | $type[$key][]="
";
981 | }
982 | }elseif ($v['type'] == 4 && $v['monitor'] == 1){
983 | //ICMP and ports check
984 | $type[$key][]=sprintf("
%s%s
",$v['@attributes']['name'],$status_2_header);
985 | if (!isset($v['icmp']) && !isset($v['port'])){
986 | $type[$key][]="Нет данных";
987 | }else{
988 | if (isset($v['icmp'])){
989 | $type[$key][]="
";
990 | $type[$key][]="";
991 | $type[$key][]="";
992 | $type[$key][]="| Тип | ";
993 | $type[$key][]="Время ответа | ";
994 | $type[$key][]="
";
995 | $type[$key][]="";
996 | $type[$key][]=sprintf("",$v['status'] > 0 ? " alarm" : "");
997 | $type[$key][]=sprintf("| %s | ",isset($v['icmp']['type']) ? $v['icmp']['type'] : "n/a");
998 | $type[$key][]=sprintf("%s | ",(isset($v['icmp']['responsetime']) && $v['icmp']['responsetime'] > 0 ) ? $v['icmp']['responsetime'] : "n/a");
999 | $type[$key][]="
";
1000 | $type[$key][]="
";
1001 | }
1002 | if (isset($v['port'])){
1003 | $type[$key][]="
";
1004 | $type[$key][]="";
1005 | $type[$key][]="";
1006 | $type[$key][]="| Тип | ";
1007 | $type[$key][]="Хост | ";
1008 | $type[$key][]="Порт | ";
1009 | $type[$key][]="Протокол | ";
1010 | $type[$key][]="Время ответа | ";
1011 | $type[$key][]="
";
1012 | $type[$key][]="";
1013 | if (!isset($v['port'][0])){
1014 | $ptmp=$v['port'];
1015 | $v['port']=array( '0' => $ptmp );
1016 | unset($ptmp);
1017 | }
1018 | foreach ($v['port'] as $kp=>$pv){
1019 | $type[$key][]=sprintf("",$v['status'] > 0 ? " alarm" : "");
1020 | $type[$key][]="| Порт | ";
1021 | $type[$key][]=sprintf("%s | ",isset($pv['hostname']) ? $pv['hostname'] : "n/a");
1022 | $type[$key][]=sprintf("%s | ",isset($pv['portnumber']) ? $pv['portnumber'] : "n/a");
1023 | $type[$key][]=sprintf("%s%s | ",isset($pv['type']) ? $pv['type'] : "",isset($pv['protocol']) ? sprintf(" (%s)",$pv['protocol']) : "");
1024 | $type[$key][]=sprintf("%s | ",(isset($pv['responsetime']) && $pv['responsetime'] > 0 ) ? $pv['responsetime'] : "n/a");
1025 | $type[$key][]="
";
1026 | }
1027 | $type[$key][]="
";
1028 | }
1029 | }
1030 | }elseif ($v['type'] == 7 && $v['monitor'] == 1){
1031 | //scripts checks
1032 | $type[$key][]=sprintf("
%s%s
",$v['@attributes']['name'],$status_2_header ? $status_2_header : " :: OK");
1033 | }else{
1034 | if ($v['monitor'] == 1){
1035 | $type[$key][]=sprintf("
%s%s
",$v['@attributes']['name'],$status_2_header);
1036 | $type[$key][]=error(sprintf("Неизвестный тип данных: %d",$v['type']));
1037 | }
1038 | }
1039 | }else{
1040 | deb("TYPE NOT SET");
1041 | deb($v);
1042 | }
1043 | }
1044 | }
1045 | }
1046 |
1047 | foreach ($type as $k=>$v){
1048 | if (count($type[$k]) > 0){
1049 | $ret[$k] = implode("\n",$type[$k]);
1050 | }
1051 | }
1052 |
1053 | return $ret;
1054 | }
1055 |
1056 | function get_bwk($bw,$num=0){
1057 | $bwm=array("байт","Кб","Мб","Гб");
1058 | $nn=0;
1059 | while($bw >= 1024){
1060 | $nn++;
1061 | if($num == 0){
1062 | $num = 2;
1063 | }
1064 | $bw/=1024;
1065 | }
1066 | return sprintf("%.".$num."f %s",$bw,isset($bwm[$nn]) ? $bwm[$nn] : "");
1067 | }
1068 |
1069 | function uptime( $sec, $what = ""){
1070 | $ret = array(
1071 | 'days' => 0,
1072 | 'hours' => 0,
1073 | 'mins' => 0,
1074 | 'secs' => 0,
1075 | 'string' => '',
1076 | );
1077 |
1078 | if( $ret['days'] = floor( $sec / 86400 ) ){
1079 | $sec -= $ret['days'] * 86400;
1080 | $ret['string'] = sprintf( "%d дн. ", $ret['days'] );
1081 | }
1082 |
1083 | if( $ret['hours'] = floor( $sec / 3600 ) ){
1084 | $sec -= $ret['hours'] * 3600;
1085 | }
1086 |
1087 | if( $ret['mins'] = floor( $sec / 60 ) ){
1088 | $sec -= $ret['mins'] * 60;
1089 | }
1090 | $ret['secs'] = $sec;
1091 |
1092 | $ret['string'] .= sprintf( "%02d:%02d:%02d", $ret['hours'], $ret['mins'], $ret['secs'] );
1093 |
1094 | if ($what == "string"){
1095 | $ret = $ret['string'];
1096 | }
1097 | return $ret;
1098 | }
1099 |
1100 | function check_version( ){
1101 | $ret = array();
1102 | $err = array();
1103 | if (!defined('VERSION') || !VERSION){
1104 | $err[]="Локальная версия неизвестна";
1105 | }
1106 |
1107 | if (count($err) == 0){
1108 | $host="mmonit-free.subnets.ru";
1109 | $port="80";
1110 | $get=sprintf("/check_version.php?version=%s",VERSION);
1111 | $text=sprintf("
Доступна новая версия ~VERSION~",$host);
1112 |
1113 | if (defined('CHECK_4_NEW_VERSION') && CHECK_4_NEW_VERSION === true){
1114 | if (!isset($_SESSION['check_version'])){
1115 | $data='';
1116 | $sock = @stream_socket_client(sprintf("tcp://%s:%d",$host,$port), $errno, $errstr, 5, STREAM_CLIENT_CONNECT);
1117 | if ($sock&&$errno==0){
1118 | @fwrite($sock, sprintf("GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: mMonit version CHECKER v0.1\r\nAccept: */*\r\n\r\n",$get,$host));
1119 | while (!@feof($sock)) {
1120 | $data.=@fgets($sock, 1024);
1121 | }
1122 | @fclose($sock);
1123 | }
1124 | if ($data){
1125 | $tmp=explode("\r\n\r\n",$data);
1126 | if (isset($tmp[1]) && check_version_number($tmp[1])){
1127 | $ret['version']=$tmp[1];
1128 | $_SESSION['check_version']=$tmp[1];
1129 | }
1130 | }
1131 | if (!isset($_SESSION['check_version'])){
1132 | $_SESSION['check_version'] = VERSION;
1133 | }
1134 | }else{
1135 | $tmp = check_version_number($_SESSION['check_version']);
1136 | if ($tmp){
1137 | $ret['version']=$tmp;
1138 | }
1139 | }
1140 |
1141 | if (isset($ret['version'])){
1142 | $ret['version']=preg_replace("/~VERSION~/",$ret['version'],$text);
1143 | }
1144 | }
1145 | }
1146 |
1147 | if (count($err) > 0){
1148 | $ret['error']=$err;
1149 | }
1150 | return $ret;
1151 | }
1152 |
1153 | function check_version_number( $version ="" ){
1154 | $ret="";
1155 | if (preg_match("/^(\d{1,3})(\.\d{1,2}){0,2}$/",trim($version))){
1156 | $my=preg_replace("/\./","",VERSION);
1157 | $cur=preg_replace("/\./","",$version);
1158 | if ( (int)$cur > (int)$my ){
1159 | $ret=$version;
1160 | }
1161 | }
1162 | return $ret;
1163 | }
1164 |
1165 | ?>
--------------------------------------------------------------------------------