Welcome Guest, Not a member yet? Register   Sign In
Server status with Codeigniter
#1

[eluser]DephNet[Paul][/eluser]
Hi Guys,

I am currently developing a site with CI that will have a server status page, for the moment to see if the server is online or not. The page will be checking several servers, currently 5 but increasing by 3 servers a month.

I am stuck on where to put the script that actual checks the servers, I have tried to include the PHP to check if the server is online in the view, but I can not see how to use the psudo-variables, that the template parser makes use of, in the PHP on the view.

The next place I tried doing this was in the controller, but I just ended up in a mess, not being able to put the result into the array that is sent to the view.

Would creating a class, that is loaded on the right page, be the best idea, and if so how would I go about it?

I have read the user guide, and have written my own simple classes, just two that loads the header and footer, but nothing like this as yet.

Sorry if this seems like a stupid question but I have been trying to get this written for a couple days now and every time I think I have it, it ends up messing the entire site up.
#2

[eluser]Watermark Studios[/eluser]
Honestly, I would create a model that represents a server (including its status). Then in the controller I would instantiate the model for each server I am checking, passing what ever parameters would be necessary to connect to the server and return the status. Then I would either drop those objects into an array to pass to the view for handling or assign specific object properties to array keys within an array. There are a number of ways to manage this. Build the model based on a table of server connection strings in a database, then just pass that object all the way to the view, loop through the object pulling out each of the servers' information. Think of it like pulling a table of users from a database and displaying them in a view. Except you are pulling a table of server connection parameters, then processing the connection and storing response in the object before passing to the view.

Hope this helps,

Ken
#3

[eluser]JHackamack[/eluser]
I've built a status board for where I work to monitor our servers. I have a database populated with the server information:

Name | DNS Name | SSL Enabled

I have two functions in the controller
index - displays the main page with server information
server_parse - curls the server and parses the results.

Right now I have a php script that lives on each that provides the uptime, cpu load, hard drive space, and other variables in a json array. The server_parse function connects to the server and curls the php script to get the json array. In the index I have a AJAX call for each server to server_parse that populates a table with the statistics.

You can always replace the curl with your favorite ssh commands to get system information without the remove file.

If this sounds like something you're looking for and need help let me know, I'd be willing to provide pointers.
#4

[eluser]Watermark Studios[/eluser]
That is a solid solution.

Why not just have the server_parse function in a server model? I'm just thinking about portability and reusability.

Otherwise, this sounds like a solid way to do it.
#5

[eluser]JHackamack[/eluser]
Well for simplify of describing it the majority of the curl stuff is in the model. You can't AJAX call a model so I have to have something in the controller.
#6

[eluser]Watermark Studios[/eluser]
Ah...so instantiating the model and passing args or static method in the controller and the data work in the model.
#7

[eluser]JHackamack[/eluser]
Usually I'll pass the server information into the model (DNS Name, SSL Enabled, etc) and then return the json array from the remote server.
#8

[eluser]DephNet[Paul][/eluser]
[quote author="JHackamack" date="1287546855"]I've built a status board for where I work to monitor our servers. I have a database populated with the server information:

Name | DNS Name | SSL Enabled[/quote]At the moment I am simply hard coding the Server Name, Hostname, IP, and Location into an array, I will however move this into a database when needed.

[quote author="JHackamack" date="1287546855"]I have two functions in the controller
index - displays the main page with server information
server_parse - curls the server and parses the results.

Right now I have a php script that lives on each that provides the uptime, cpu load, hard drive space, and other variables in a json array. The server_parse function connects to the server and curls the php script to get the json array. In the index I have a AJAX call for each server to server_parse that populates a table with the statistics.[/quote]At the moment I am concentrating on getting this to display if the server is online or offline, in future I would be looking to add things like this in.

I will be honest and say that I have never used AJAX and/or JSON at all and so I guess my first port of call is reading up on AJAX and JSON

[quote author="JHackamack" date="1287546855"]You can always replace the curl with your favorite ssh commands to get system information without the remove file.

If this sounds like something you're looking for and need help let me know, I'd be willing to provide pointers.[/quote]If you could that would be great, Thank you. I don't expect you to do anything on this, but I must warn you, as I said above, I am a novice when it comes to AJAX and JSON so please go carefully.
#9

[eluser]JHackamack[/eluser]
You can easily do everything statically without AJAX, I just found that if a server takes a long time to load your whole page will load slow because its reliant on one server. If you're interested in just if a server is reachable you can use the PING command utility from the command line. I did some quick google searching and found:


<?php
function ping($host) {
exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)Wink, $res, $rval);
return $rval === 0;
}

$hosts_to_ping = array('10.140.206.123', '127.0.0.1');
?>

<ul>
&lt;?php foreach ($hosts_to_ping as $host): ?&gt;
<li>
&lt;?php echo $host; ?&gt;
&lt;?php $up = ping($host); ?&gt;
(<img src="&lt;?php echo $up ? 'on' : 'off'; ?&gt;"
alt="&lt;?php echo $up ? 'up' : 'down'; ?&gt;">)
</li>
&lt;?php endforeach; ?&gt;
</ul>

Here: http://www.dynamicdrive.com/forums/showt...hp?t=37370


You can just build an array of ip addresses or DNS names and you should be able to get an up or down graphic to appear for each.


I'd say get it working how you envision it, because thats the itch you want to scratch. If you get it built and find you want something more you can delve into all the other areas of information reporting.
#10

[eluser]DephNet[Paul][/eluser]
Hi JHackamack,

Thanks for that, that code is a bit cleaner than the code I was trying to use.

My question really was where should I put it? In the controller, and try and build a second array from the results of the first, or, as Watermark Studios said, put it in the model?




Theme © iAndrew 2016 - Forum software by © MyBB