0) { return TRUE; } return FALSE; } function delete_service_link($linkid) { $sql="delete from services_links where linkid=$linkid"; return DBexecute($sql); } function delete_service($serviceid) { $sql="delete from services_links where servicedownid=$serviceid or serviceupid=$serviceid"; $result=DBexecute($sql); if(!$result) { return $result; } // delete service permisions DBexecute('delete from rights where name=\'Service\' and id='.$serviceid); $sql="delete from services where serviceid=$serviceid"; return DBexecute($sql); } # Return TRUE if triggerid is a reason why the service is not OK # Warning: recursive function function does_service_depend_on_the_service($serviceid,$serviceid2) { # echo "Serviceid:$serviceid Triggerid:$serviceid2
"; $service=get_service_by_serviceid($serviceid); # echo "Service status:".$service["status"]."
"; if($service["status"]==0) { return FALSE; } if($serviceid==$serviceid2) { if($service["status"]>0) { return TRUE; } } $sql="select serviceupid from services_links where servicedownid=$serviceid2 and soft=0"; # echo $sql."
"; $result=DBselect($sql); while($row=DBfetch($result)) { if(does_service_depend_on_the_service($serviceid,$row["serviceupid"]) == TRUE) { return TRUE; } } return FALSE; } function service_has_parent($serviceid) { $sql="select count(*) as cnt from services_links where servicedownid=$serviceid"; $result=DBselect($sql); $row=DBfetch($result); if($row["cnt"]>0) { return TRUE; } return FALSE; } function service_has_no_this_parent($parentid,$serviceid) { $sql="select count(*) as cnt from services_links where serviceupid=$parentid and servicedownid=$serviceid"; $result=DBselect($sql); $row=DBfetch($result); if($row["cnt"]>0) { return FALSE; } return TRUE; } function add_service_link($servicedownid,$serviceupid,$softlink) { if( ($softlink==0) && (is_service_hardlinked($servicedownid)==true) ) { return false; } if($servicedownid==$serviceupid) { error("cannot link service to itself."); return false; } $linkid=get_dbid("services_links","linkid"); $sql="insert into services_links (linkid,servicedownid,serviceupid,soft) values ($linkid,$servicedownid,$serviceupid,$softlink)"; $result=DBexecute($sql); if(!$result) return $result; return $linkid; } function update_service_link($linkid,$servicedownid,$serviceupid,$softlink) { if( ($softlink==0) && (is_service_hardlinked($servicedownid)==true) ) { return false; } if($servicedownid==$serviceupid) { error("cannot link service to itself."); return false; } $sql="update services_links set servicedownid=$servicedownid, serviceupid=$serviceupid, soft=$softlink where linkid=$linkid"; return dbexecute($sql); } function get_last_service_value($serviceid,$clock) { $sql="select count(*) as cnt,max(clock) as maxx from service_alarms where serviceid=$serviceid and clock<=$clock"; // echo " $sql
"; $result=DBselect($sql); $row=DBfetch($result); if($row["cnt"]>0) { $sql="select value from service_alarms where serviceid=$serviceid and clock=".$row["maxx"]; $result2=DBselect($sql); // Assuring that we get very latest service value. There could be several with the same timestamp // $value=DBget_field($result2,0,0); while($row2=DBfetch($result2)) { $value=$row2["value"]; } } else { $value=0; } return $value; } function calculate_service_availability($serviceid,$period_start,$period_end) { $sql="select count(*),min(clock),max(clock) from service_alarms where serviceid=$serviceid and clock>=$period_start and clock<=$period_end"; $sql="select clock,value from service_alarms where serviceid=$serviceid and clock>=$period_start and clock<=$period_end"; $result=DBselect($sql); // -1,0,1 $state=get_last_service_value($serviceid,$period_start); $problem_time=0; $ok_time=0; $time=$period_start; while($row=DBfetch($result)) { $clock=$row["clock"]; $value=$row["value"]; $diff=$clock-$time; $time=$clock; #state=0,1 (OK), >1 PROBLEMS if($state<=1) { $ok_time+=$diff; $state=$value; } else { $problem_time+=$diff; $state=$value; } } // echo $problem_time,"-",$ok_time,"
"; if(!DBfetch($result)) { if(get_last_service_value($serviceid,$period_start)<=1) { $ok_time=$period_end-$period_start; } else { $problem_time=$period_end-$period_start; } } else { if($state<=1) { $ok_time=$ok_time+$period_end-$time; } else { $problem_time=$problem_time+$period_end-$time; } } // echo $problem_time,"-",$ok_time,"
"; $total_time=$problem_time+$ok_time; if($total_time==0) { $ret["problem_time"]=0; $ret["ok_time"]=0; $ret["problem"]=0; $ret["ok"]=0; } else { $ret["problem_time"]=$problem_time; $ret["ok_time"]=$ok_time; $ret["problem"]=(100*$problem_time)/$total_time; $ret["ok"]=(100*$ok_time)/$total_time; } return $ret; } function get_service_status_description($status) { $desc="OK"; if($status==5) { $desc="Disaster"; } elseif($status==4) { $desc="Serious".SPACE."problem"; } elseif($status==3) { $desc="Average".SPACE."problem"; } elseif($status==2) { $desc="Minor".SPACE."problem"; } elseif($status==1) { $desc="OK"; } return $desc; } function get_num_of_service_childs($serviceid) { $sql="select count(*) as cnt from services_links where serviceupid=$serviceid"; $result=DBselect($sql); $row=DBfetch($result); return $row["cnt"]; } function get_service_by_serviceid($serviceid) { $sql="select * from services where serviceid=$serviceid"; $result=DBselect($sql); $res = DBfetch($result); if(!$res) { error("No service with serviceid=[$serviceid]"); return FALSE; } return $res; } function get_services_links_by_linkid($linkid) { $result=DBselect("select * from services_links where linkid=$linkid"); $res = DBfetch($result); if(!$res) { error("No service linkage with linkid=[$linkid]"); return FALSE; } return $res; } function algorithm2str($algorithm) { if($algorithm == SERVICE_ALGORITHM_NONE) { return S_NONE; } elseif($algorithm == SERVICE_ALGORITHM_MAX) { return S_MAX_OF_CHILDS; } elseif($algorithm == SERVICE_ALGORITHM_MIN) { return S_MIN_OF_CHILDS; } return S_UNKNOWN; } ?>