DjMikeS Posted March 25, 2009 Share Posted March 25, 2009 Hi all, I'm in the process of building a rather complex web app. It is going to be an IT helpdesk with a link to the monitoring system so that failures are automatically logged and processed. I'm trying to work with modules (one for the monitoring, one for the calls, users etc.) but as I progress, I notice that I'm starting to lose control a bit as I create more and more files. Surely there must be a better way to do this. I have been reading up on PHP MVC's but for some reason this doesn't seem suitable for my project. Or maybe (probably) I'm just thinking in the wrong direction. Could you give me some pointers? Here's an example of the monitoring module: Each module has it's own index.php which is included by the main index.php <?php require ($site_path . $modules_dir ."nagios/classes/nagios_status.pclass"); $cntBuffer = New DataBuffer(); require ($site_path . $modules_dir ."nagios/contextmenu.php"); $nagiosStatus = New nagiosStatus($_SESSION['id']); require ($site_path.$modules_dir."nagios/popups/add_hosts.php"); echo $cntPopUp.' <div class="menu_bar"><div style="width: 100%; text-align: center; font-size: 14px; margin-top: 1px;"><strong>Nagios quick status overview for '. date("d-m-Y H:i", $nagiosStatus->LastUpdateTime) .'</strong></div></div> <div class="menu_content">'; if (isset($_GET['option'])) { switch($_GET['option']) { case "hosts": $arrLastHosts = $nagiosStatus->getHosts(); require ($site_path.$modules_dir.$registry['module']."/functions/showHosts.php"); echo $cntShowHosts; break; case "services": if (isset($_GET['host'])) { $host = mysql_real_escape_string($_GET['host']); $arrLastServices = $nagiosStatus->getServicesByHost($host); } else { $arrLastServices = $nagiosStatus->getServices(); } require ($site_path.$modules_dir.$registry['module']."/functions/showServices.php"); echo $cntShowServices; break; case "settings": break; case "history": $host = mysql_real_escape_string($_GET['host']); if (isset($_GET['service'])) { $service = mysql_real_escape_string($_GET['service']); $arrServiceHistory = $nagiosStatus->getServiceHistory($host, $service); } else { $arrHostHistory = $nagiosStatus->getHostHistory($host); } require ($site_path.$modules_dir.$registry['module']."/functions/showHistory.php"); echo $cntShowHistory; break; default: break; } } else { $arrLastHosts = $nagiosStatus->getHosts(); $arrLastServices = $nagiosStatus->getServices(); require ($site_path.$modules_dir.$registry['module']."/functions/showStatus.php"); echo $cntShowStatus; } echo '</div>'; $cntBody = $cntBuffer->close(); ?> The nagios class: <?php class nagiosStatus { function __construct($uid) { $this->db = new Database(); $this->uid = $uid; $getLastUpdate = mysql_query ("SELECT id,timestamp FROM nagios_program ORDER BY id DESC LIMIT 0,1"); $arrLastUpdate = mysql_fetch_array($getLastUpdate); $getRemHosts = mysql_query ("SELECT id,host FROM nagios_remhosts WHERE uid='$this->uid'"); $arrRemHosts = array(); $this->arrRemHosts = $arrRemHosts = mk_multiarray($getRemHosts); $arrRemHost = array(); $i=0; foreach ($arrRemHosts as $val) { $arrRemHost[$i] = $val[1]; $i++; } $this->LastUpdateID = $arrLastUpdate[0]; $this->LastUpdateTime = $arrLastUpdate[1]; $this->arrRemHosts = $arrRemHosts; $this->arrRemHost = $arrRemHost; } function getHosts() { if (count($this->arrRemHost)-1 > 0) { return $arrLastHosts = $this->db->select ("SELECT * FROM nagios_hosts WHERE tid = '$this->LastUpdateID' AND host NOT IN ('".implode("', '", $this->arrRemHost)."')"); } else { return $arrLastHosts = $this->db->select ("SELECT * FROM nagios_hosts WHERE tid = '$this->LastUpdateID'"); } //return $arrLastHosts = mk_multiarray($getLastHosts); } function getServices() { if (count($this->arrRemHost)-1 > 0) { $getLastServices = mysql_query ("SELECT * FROM nagios_services WHERE tid = '$this->LastUpdateID' AND host NOT IN ('".implode("', '", $this->arrRemHost)."') ORDER BY host"); } else { $getLastServices = mysql_query ("SELECT * FROM nagios_services WHERE tid = '$this->LastUpdateID' ORDER BY host"); } return $arrLastServices = mk_multiarray($getLastServices); } function getServicesByHost($strHost) { $getLastServices = mysql_query ("SELECT * FROM nagios_services WHERE tid = '$this->LastUpdateID' AND host = '$strHost'"); return $arrLastServices = mk_multiarray($getLastServices); } function getServiceHistory($host, $service) { $getServiceHistory = mysql_query ("SELECT * FROM nagios_services WHERE host = '$host' AND service = '$service' ORDER BY host,last_check DESC"); return $arrServiceHistory = mk_multiarray($getServiceHistory); } function getHostHistory($host) { $getHostHistory = mysql_query ("SELECT * FROM nagios_hosts WHERE host = '$host' ORDER BY last_check DESC"); return $arrHostHistory = mk_multiarray($getHostHistory); } } ?> And this is showHosts.php <?php $cntBuffer = new Databuffer(); echo '<div class="cnt_bar"><div style="float: right;"><a href="#" id="toggle" div="naghosts"><img src="'.$site_url.'img/'.$icon_dir.'16/down.png" /></a></div>Total number of hosts: '. count($arrLastHosts).'</div> <div class="cnt" id="naghosts"><table> <tr><td width="5%">Type:</td><td width="10%">Host:</td><td width="10%">Status:</><td width="20%">Last checked:</td><td width="20%">In this state since:</td><td width="35%">Description:</td></tr>'; foreach ($arrLastHosts as $arrLastHost) { echo '<tr onmouseover="this.style.background=\'#40E0D0\';" onmouseout="this.style.background=\'\';" id="'.$arrLastHost[3].'" class="nagHosts"><td width="5%">'.$arrLastHost[2].'</td><td width="10%">'.$arrLastHost[3].'</td><td width="10%">'.$arrLastHost[4].'</td><td width="20%">'.date("d-m-Y H:i:s", $arrLastHost[5]).'</td><td width="20%">'.date("d-m-Y H:i:s", $arrLastHost[6]).'</td><td width="35%">'.$arrLastHost[7].'</td></tr>'; } echo '</table></div>'; $cntShowHosts = $cntBuffer->close(); ?> I appreciate any thoughts on the project. Also, I'm writing this whole project with Notepad++, which is good, but I think maybe a PHPide is better suitable...what do you recommend? Link to comment https://forums.phpfreaks.com/topic/151035-simplify-application-and-reduce-file-count/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.