Beanmen

  • Mar 18, 2019
  • Joined Feb 22, 2019
  • laserlight

    Currently without the cron I'm running it like this:

      <?php
    
    require_once "config/config.php";
    
    $sql = "SELECT * FROM deployments";
    	if($result = mysqli_query($link, $sql)){
    		if(mysqli_num_rows($result) > 0){
    			while($row = mysqli_fetch_array($result)){
    				?>
    					<tr>
    						<td> <?php echo $row['id'] ?></td>
    						<td> <?php echo $row['server'] ?></td>
    						<td> <?php echo $row['name'] ?></td>
    						<td> <?php echo $row['url']?> <a href="http://<?php echo $row['url']?>" target="_blank" style="color: grey;"><span class="fa fa-external-link"></span></a></td>
    						<td></td>
    				<?php
    					  $url = $row['url'];
    					  $ch = curl_init($url);
    					  curl_setopt($ch, CURLOPT_NOBODY, true);
    					  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    					  curl_exec($ch);
    					  $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    					  curl_close($ch);
    					  if (200==$retcode) {
    						    echo "<td><span class='badge badge-success'>LIVE</span></td>";
    					  } else {
    						    echo "<td><span class='badge badge-danger'>DOWN</span></td>";
    					    $path_to_file = './emailtemplate.html';
    					    $file_contents = file_get_contents($path_to_file);
    					    $file_contents = str_replace("depplaceholder","$url",$file_contents);
    				        $to = "---";
    				        $subject = "$url down";
    				        $headers = "From:Deployment Monitor <--->" . "\r\n";
    				        $headers .= 'MIME-Version: 1.0' . "\r\n";
    				        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    					$message = $file_contents;
    
    			        	mail($to,$subject,$message,$headers);
    				  }
    				 ?>
    
    					<td><a href="config/remove.php?id=<?php echo $row['id']; ?>" style="color: grey;"><span class="fa fa-trash"></span></a></td>
    				</tr>
    	<?php
    				}
    				mysqli_free_result($result);
    			} else{
    				echo "No records matching your query were found.";
    			}
    	} else{
    			echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    	}
    
    	?>

    I'm still new tot php, getting the hang of it but I'm not sure on how to make the cron script run the curl ping, post the data to a row that applies to:

    	<tr>
    	  <td> <?php echo $row['id'] ?></td>
    	  <td> <?php echo $row['server'] ?></td>
    	  <td> <?php echo $row['name'] ?></td>
    	  <td> <?php echo $row['url']?> </td>
              >>>>>><td> <?php echo $row['status']?> </td><<<<< cron would push data to this table per url after pinging to either LIVE or DOWN and would post the data in this <td>
           </tr>

    Sorry for asking a lot lol I'm just confused and google isn't helping since there seems no answer to my specific question.

    • Hi,

      I've been searching the web for a actual explanation on what a cron job does, I get on how to set it but I'm not completely sure on how to make it run a script.

      I might sound a bit dumb but I just can't get my head around it, so in example:

      I've got my website, 100+ urls being pinged (currently on accessing the site so this freezes the website for aprox. 40 sec) I want this to happen in the background and make a cron run the script to ping every 2 minutes.

      Does this mean I need to create a separate "script.php" with the curl ping script in it that used to be and link this somehow to the original page where the ping process was done before or not.

      Or does it mean that I create a cron script that auto refreshes the page every 2 minutes?

      As I said I'm getting a little confused because all the tutorials I find on the web just explain how to create the cron but not how to link a script to replace a already existing process.

      Sorry for the silly question lol

      • NogDog

        It's under control! haha I couldn't get the code to show as code on the page here, so I copied it from my php file (which doesn't look like this lol) and I guess it got a bit messed up.

        • NogDog

          I've got it working lol

                      <?php
          
          	require_once "config/config.php";
          
          	$sql = "SELECT * FROM deployments";
          	if($result = mysqli_query($link, $sql)){
          			if(mysqli_num_rows($result) > 0){
          				while($row = mysqli_fetch_array($result)){
          					?>
          							<tr>
          									<td> <?php echo $row['id'] ?></td>
          									<td> <?php echo $row['server'] ?></td>
          									<td> <?php echo $row['name'] ?></td>
          									<td> <?php echo $row['url'] ?></td>
          									<td> <?php echo $row['port'] ?></td>
          
          
          									<?php
          									  $url = $row['url'];
          									  $ch = curl_init($url);
          									  curl_setopt($ch, CURLOPT_NOBODY, true);
          									  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
          									  curl_exec($ch);
          									  $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
          									  curl_close($ch);
          									  if (200==$retcode) {
          									    echo "<td><span class='badge badge-success'>LIVE</span></td>";
          									  } else {
          									    echo "<td><span class='badge badge-danger'>DOWN</span></td>";
          									    $path_to_file = './emailtemplate.html';
          									    $file_contents = file_get_contents($path_to_file);
          									    $file_contents = str_replace("depplaceholder","$url",$file_contents);
          									    file_put_contents($path_to_file,$file_contents);
          
          									        $to = "";
          									        $subject = "$url down";
          									        $headers = "From: Deployment Monitor" . "\r\n";
          									        $headers .= 'MIME-Version: 1.0' . "\r\n";
          									        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
          									        $message = file_get_contents('./emailtemplate.html');
          
          									      //	mail($to,$subject,$message,$headers);
          									  }
          									  ?>
          
          
          									<td><a href="config/remove.php?id=<?php echo $row['id']; ?>"><span class="fa fa-trash"></span></a></td>
          							</tr>
          	<?php
          					}
          					// Free result set
          					mysqli_free_result($result);
          			} else{
          					echo "No records matching your query were found.";
          			}
          	} else{
          			echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
          	}
          
          	?>
          • Weedpacket

            I know it is completely different because I changed the view of the website, so the old code with the array was working fine, all the servers were shown in this list, I changed the website and added a add server button that adds the data to a db, this data is shown on the website, but I cant get the curl to ping the output data, I know I'm not making sense and that I'm wrong somewhere but I can't see what goes wrong.

            In my head the curl pings the output from the table, not the data on the table before that data is shown as a output on the website in a <td> tag.

            <?php
            
            							require_once "config/config.php";
            
            							$sql = "SELECT * FROM deployments";
            							if($result = mysqli_query($link, $sql)){
            									if(mysqli_num_rows($result) > 0){
            										while($row = mysqli_fetch_array($result)){
            											?>
            													<tr>
            															<td> <?php echo $row['id'] ?></td>
            															<td> <?php echo $row['server'] ?></td>
            															<td> <?php echo $row['name'] ?></td>
            															<td> <?php echo $row['url'] ?></td>
            															<td> <?php echo $row['port'] ?></td>
            <?php
            	function url_test( $url ) {
              $timeout = 10;
              $ch = curl_init();
              curl_setopt ( $ch, CURLOPT_URL, $url );
              curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
              curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
              $http_respond = curl_exec($ch);
              $http_respond = trim( strip_tags( $http_respond ) );
              $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
              if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {
                return true;
              } else {
                // return $http_code;, possible too
                return false;
              }
              curl_close( $ch );
            }
            
            $website = "www.www.com";
            if( !url_test( $website ) ) {
              echo "<td><span class='badge badge-danger'>DOWN</span></td>";
            }
            else { echo "<td><span class='badge badge-success'>LIVE</span></td>"; }
            
            ?>
            															<td><a href="config/remove.php?id=<?php echo $row['id']; ?>"><span class="fa fa-trash"></span></a></td>
            													</tr>
            							<?php
            											}
            											// Free result set
            											mysqli_free_result($result);
            									} else{
            											echo "No records matching your query were found.";
            									}
            							} else{
            									echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
            							}
            
            							?>
            • NogDog

              All websites I find pull the data from 1 website i.e.

              $website = "www.example.com"; << This is not one website but about 160, this data is pulled from a database
              if( !url_test( $website ) ) {
              echo $website ." is down!";
              }
              else { echo $website ." functions correctly."; }

              Weedpacket

              My old code looks like this:

              <?php
              						$tests = array
              						(
                                                                  `array list`
              						);
              
              						// Set counter
              						$i = 1;
              
              						// For each test create a tr
              						foreach($tests as $test => $testProperties){
              
              							echo "<tr>";
              
              							// Display test ID
              							echo "<td>$i</td>";
              
              
              							echo "<td>".$testProperties[0]."</td>";
              							echo "<td>".$testProperties[1]."</td>";
              
              							if($socket =@ fsockopen($testProperties[1], 80, $errno, $errstr, 1)) {
              
              								echo "<td><span class='badge badge-success'>LIVE</span></td>";
              								fclose($socket);
              
              							}
              							else {
              
              								echo "<td><span class='badge badge-danger'>DOWN</span></td>";
              
              							}
              
              							echo "</tr>";
              
              							// Increase counter
              							$i++;
              
              						}
              
                        ?>

              I changed this code to the following but the fsock suddenly didn't ping the servers any longer (this code isn't including the curl ping bit of code it's just the way the page is showing):

              <?php
              
              							require_once "config/config.php";
              
              							$sql = "SELECT * FROM deployments";
              							if($result = mysqli_query($link, $sql)){
              									if(mysqli_num_rows($result) > 0){
              										while($row = mysqli_fetch_array($result)){
              											?>
              													<tr>
              															<td> <?php echo $row['id'] ?></td>
              															<td> <?php echo $row['server'] ?></td>
              															<td> <?php echo $row['name'] ?></td>
              															<td> <?php echo $row['url'] ?></td>
              															<td> <?php echo $row['port'] ?></td>
              															<td><span class='badge badge-warning'>LIVE</span></td>
              															<td><a href="config/remove.php?id=<?php echo $row['id']; ?>"><span class="fa fa-trash"></span></a></td>
              													</tr>
              							<?php
              											}
              											// Free result set
              											mysqli_free_result($result);
              									} else{
              											echo "No records matching your query were found.";
              									}
              							} else{
              									echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
              							}
              
              							?>

              How do I make the fsock or curl ping all the servers shown in the database under the "url" table, as I can't get them to show.

            • Hi I'm new to this forum so sorry if I do it wrong!

              I've got a list of 160 URL's in a db table and the rows are shown like:

              <?php

              require_once "config/config.php";

              $sql = "SELECT * FROM deployments";
              if($result = mysqli_query($link, $sql)){
              if(mysqli_num_rows($result) > 0){
              while($row = mysqli_fetch_array($result)){
              ?>
              <tr>
              <td> <?php echo $row['id'] ?></td>
              <td> <?php echo $row['server'] ?></td>
              <td> <?php echo $row['name'] ?></td>
              <td> <?php echo $row['url'] ?></td>
              <td> <?php echo $row['port'] ?></td>
              <td><span class='badge badge-warning'>LIVE</span></td>
              <td><a href="config/remove.php?id=<?php echo $row['id']; ?>"><span class="fa fa-trash"></span></a></td>
              </tr>
              <?php
              }
              mysqli_free_result($result);
              } else{
              echo "No records matching your query were found.";
              }
              } else{
              echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
              }

              ?>

              Is it possible to use fsoc or curl to ping the URL's pulled from the db after made visible on the webpage with line:
              <td> <?php echo $row['url'] ?></td>

              I've tried this but I can't seem to get it to work, I had it working when I pulled the data from an array but I want the data to come from the db.

              Thank you, and if I need to change something let me know and I will edit my post.