Nebox Posted August 31, 2016 Share Posted August 31, 2016 Hello everyone, Could anyone show me an example or recommend some simple script to record server uptime statistics during 7 days, so i could use it automatically for multiple ip:port located in database and than display as chart on selected server page.I need it to be as simple as possible, i need at least an example just to understand the right way doing it so i could build this "plugin" for my website. Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/ Share on other sites More sharing options...
requinix Posted August 31, 2016 Share Posted August 31, 2016 It may be because I'm drunk ( ) but I don't understand what you're asking. Do you want to chart the server's uptime over a period of a week? Does the output from `uptime` help? Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536880 Share on other sites More sharing options...
Nebox Posted August 31, 2016 Author Share Posted August 31, 2016 (edited) It may be because I'm drunk ( ) but I don't understand what you're asking. Do you want to chart the server's uptime over a period of a week? Does the output from `uptime` help? I wanna do this kind of chart, i need some example of the php side for recording uptime data etc I just cant understand how should i write the script =/ ( server uptime monitoring ) getting timestamp and server status for the ip like every hour and than in database i'll have a lot of servers with 24 records every day ? i'm really confused on how to do it, this is why i need some example or explanation :C ps. there will be around 100-200 ips in the database so i have to think of something really simple and optimized Edited August 31, 2016 by Nebox Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536881 Share on other sites More sharing options...
requinix Posted August 31, 2016 Share Posted August 31, 2016 (edited) "Really simple and optimized" isn't going to do it there. Uptime for a particular service (like a web server) is measured by when the service is accessible. To monitor the service yourself you're going to need something that can evaluate the status of the service. For example, with a web server you'll need something to "ping" the server periodically. Such as a cron job on a one minute timer. When it connects successfully the service is "up", and when it does not the service is "down". Recording that information could go any number of ways. Times 100-200 is not going to be simple, because connecting to that many services could easily take more time than you have between measurements. So that means either multiple "machines" measuring uptime or relaxing your measurement interval. Off hand, one way to store the results is to stick a record in the database per service per minute indicating whether it's online, then using a periodic (daily, weekly) job to summarize that information. Such as calculating uptime for a particular interval, and probably removing the individual measurements for the sake of storage. Or another method would be to say that each per-minute online measurement affords 1 units of uptime (and each day is, eg, 1440 units long), and you can simply accumulate units for each interval: INSERT INTO uptime (machine, service, day, uptime) VALUES (DATE(), 1) ON DUPLICATE KEYS UPDATE uptime = uptime + 1(where the machine+service+day is a unique key and you count uptime as 1=online and 0=offline) Edited August 31, 2016 by requinix no apologies for typos or other nonsensical grammar Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536887 Share on other sites More sharing options...
gizmola Posted August 31, 2016 Share Posted August 31, 2016 If I understand you, you will have a table of Servers with an IP address and port#. What are these servers running at these ports? It sounds like you want to write a command line php script that goes through the Server table and for each server, it will attempt to establish a connection. If that connection is "successful" it will create a row in a table that relates to the server table indicating either that the connection was a success, or there was a failure. For a number of reasons this is non-trivial, both in terms of the actual coding involved, not to mention the fact that the server must instantiate a large number of timed connections and be able to recover from failures or timeouts. Finally and this is probably the simplest part, but still not without complexity -- you want a graph/UI and there are numerous different ways to generate that. Ok, so a few tips: 1. Begin by writing a simple command line php script that takes as parameters, the IP and port#. -This script will then do the job of attempting to attach to the remote server. -It will then record the result in a table and exit. -The tried and true way to handle this type of app would be to use the cURL extension, which is documented here: http://php.net/manual/en/book.curl.php Curl has many options, and ways to limit the amount of time it should try and connect to a server. 2. Once you have #1 you can write a script that goes through your database of #'s and calls script #1 asynchronously. You want to have some controls in there that batch the number of seperate #1 scripts you have running within a period of time. For example, you might want the script to try 20 ip's at a time, then sleep for 30 seconds to allow for those scripts to complete, depending on the timeouts you find acceptable in your #1 script. 3. Display of your graphs will be handled separately. Approach this like any other standard website. You can generate the graphs as images using a graphing package that lets you generate .png or .jpg, or using something like Rddtool, or as is increasingly the case these days, you can use a javascript graphing library. Here's a page with a ton of different ones: http://www.jsgraphs.com/ I have personally used HighCharts in the recent past and found it to be high quality and easy to use. If you have specific questions (with code) feel free to follow up. Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536888 Share on other sites More sharing options...
Nebox Posted August 31, 2016 Author Share Posted August 31, 2016 Oh requinix Its almost what i wanted to know, but isn't that weird adding in uptime +1 instead of writing the current time? Well actually i guess this method is the most optimized since 1 server row every day will be created, and i don't need exact hours anyway. gizmola, I always used curl on two-end scripts ( 1 part located on 1 server and other part on other ), are you sure i should use curl and not just simple pinging? the second thing you said is very interesting, thats a really good thing to do since we don't wanna overload anything right, but how ? ;o if you could show a small example i would really appreciate it. As for 3rd thing, chart isn't a problem, i'll use google or bootstrap charts for this one.Tonight i'll start coding the script Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536891 Share on other sites More sharing options...
requinix Posted August 31, 2016 Share Posted August 31, 2016 Well actually i guess this method is the most optimized since 1 server row every day will be created, and i don't need exact hours anyway.You're only checking once per day? That doesn't sound right. Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536893 Share on other sites More sharing options...
Nebox Posted August 31, 2016 Author Share Posted August 31, 2016 You're only checking once per day? That doesn't sound right. Nah, i kinda quick-checked the sql you showed me, and started thinking that it might be a nice idea of making a total amount for 24 hours ( lats say 48 if its checked every 30 min ), than in the chart i could generate percentage out of the total number, guess i messed up a little Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536894 Share on other sites More sharing options...
gizmola Posted August 31, 2016 Share Posted August 31, 2016 Oh requinix Its almost what i wanted to know, but isn't that weird adding in uptime +1 instead of writing the current time? Well actually i guess this method is the most optimized since 1 server row every day will be created, and i don't need exact hours anyway. gizmola, I always used curl on two-end scripts ( 1 part located on 1 server and other part on other ), are you sure i should use curl and not just simple pinging? the second thing you said is very interesting, thats a really good thing to do since we don't wanna overload anything right, but how ? ;o if you could show a small example i would really appreciate it. As for 3rd thing, chart isn't a problem, i'll use google or bootstrap charts for this one. Tonight i'll start coding the script Well again, you have to answer the question I originally posed. If you ping a server there are several issues. First off, ping by default uses icmp packets, and these packets are often dropped by firewalls. Secondly, all a successful ping tells you is that the IP layer of the server is functioning. Thirdly, ping does not deal with ports. When you bring up the issue of ports, you are indicating that ping is not sufficient. For example, if you are trying to determine the availability of a web server, then you would want to at least read the HTTP header information to determine that the server is both accessible and operational. Again you didn't say that. As for your specific question, once you have your "check a single server" script, you can call that script in another "batch" or control script by using one of the system/exec calls (see http://php.net/manual/en/function.exec.php) as an example. Under *nix, when you call a script you can have it run in the background by using the '&' character. This essentially will give you a way of spawning off a number of different "#1" scripts and immediately sending them to process in the background (asynchronously). If I were doing this, I'd start with some components that will save time and increase the reliability of your solution. Off the top of my head you should look at: http://symfony.com/doc/current/components/console.html http://symfony.com/doc/current/components/process.html https://github.com/Seldaek/monolog Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536895 Share on other sites More sharing options...
Nebox Posted August 31, 2016 Author Share Posted August 31, 2016 I guess i just don't know how to explain what i actually want to do so i'll just show you an example.I want to make a plugin for website with bunch of private gaming servers ( right now its only Counter Strike servers ) basically game servers listing website. The closest thing to what i want to do is this -> http://minecraftservers.org/server/119693 click on right column on "Stats" and than select Uptime. so you don't waste time searching the buttons here is a screenshot where to click As you see this website top has a lot of servers registered, i have something similar Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536899 Share on other sites More sharing options...
gizmola Posted August 31, 2016 Share Posted August 31, 2016 Ok, so to that end, everything that's been advised is still relevant. Probably it would be advisable for you to store with each monitoring row, a column that indicates the type of server it is. In your case, initially that type would only be "counterstrike" but in the future it could be other things. The next question I would have is, when you connect to a counterstrike server, what type of conversation does the client and server have. A quick google found this page: https://developer.valvesoftware.com/wiki/Server_queries It's quite possible you could build this into your system. Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536914 Share on other sites More sharing options...
Nebox Posted August 31, 2016 Author Share Posted August 31, 2016 (edited) Ok, so to that end, everything that's been advised is still relevant. Probably it would be advisable for you to store with each monitoring row, a column that indicates the type of server it is. In your case, initially that type would only be "counterstrike" but in the future it could be other things. The next question I would have is, when you connect to a counterstrike server, what type of conversation does the client and server have. A quick google found this page: https://developer.valvesoftware.com/wiki/Server_queries It's quite possible you could build this into your system. Well thats used for getting information on server's current status ( such as map and players online ) thats a totally different story i thought about checking the status with fsockopen for the monitoring for example $current_status = ''; $server_status = @fsockopen($TMPL['server_ip'], $TMPL['server_port'], $errno, $errstr, 1); if($server_status >= 1){ $TMPL['current_status'] = 'Online'; fclose($server_status); }else{ $TMPL['current_status'] = 'Offline'; } I actually have all the features except the monitoring, so yeah, my goal is to make a chart with server status history by percentage Basically my problem was is lack of knowledge of how should i record the status to the database, to maybe a json file or db ( mysql ) or what should it actually record during 24 hour period, like do i record the exact time etc, how should my table designed ( ip | port | timestamp | status ) o.o also i'm still not sure of how do i make script run automatically without leaving the script open and exploitable in case if someone will figure out somehow to send packets to the php file located link Edited August 31, 2016 by Nebox Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536915 Share on other sites More sharing options...
gizmola Posted August 31, 2016 Share Posted August 31, 2016 First I don't agree with your assessment. The A2_INFO portion of the protocol is what I would suggest you use. Making a socket connection (or not) tells you nothing about what is running on that socket. By having intelligent code that actually queries the cogs server, you determine that the server is functioning, what you expect it to be, and in good health. I found this in no time: https://github.com/xPaw/PHP-Source-Query Most of the problem is solved. Your system needs at least 2 tables: "server" id int unsigned (primary key) auto_increment name: varchar IP: varchar Port: int "server_status" id int unsigned (primary key) auto_increment server_id: int unsigned (foreign key containing server.id} status: char[1] (O for Ok, D for Down) createdAt: timestamp You will query SELECT * from server and iterate through every row in your master script. Your detail script which is run by the master script will insert a row in server_status Timestamp can be omitted from the query, and it will be filled in with the current server timestamp value. When creating the graphs, you simply need to do some sql to get the range of rows in server_status needed to create the graph for the period you desire. The information in http://www.gizmola.com/blog/archives/51-Exploring-Mysql-CURDATE-and-NOW.-The-same-but-different..html should help you figure out how you can do directly in mysql. For these types of purposes timestamps are compatible with datetime columns. Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536921 Share on other sites More sharing options...
Nebox Posted August 31, 2016 Author Share Posted August 31, 2016 Thanks, Well intelligent code is a problem since the users that post their server to the listing will have to do something in their side ( in case its curl responding ) which is a little bad idea because more often people are not willing to do anything except giving the best ever description about their game server. Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536928 Share on other sites More sharing options...
gizmola Posted August 31, 2016 Share Posted August 31, 2016 Thanks, Well intelligent code is a problem since the users that post their server to the listing will have to do something in their side ( in case its curl responding ) which is a little bad idea because more often people are not willing to do anything except giving the best ever description about their game server. You don't seem to have followed the idea here. The servers being pinged don't need to do anything. Your server is the one that would be attempting to connect to theirs and recording the response. Quote Link to comment https://forums.phpfreaks.com/topic/302048-uptime-history-simplest-possible-script/#findComment-1536930 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.