System statistics reporting using Laravel

The Laravel web framework comes with a lot of powerful tools and one of them it the Task Scheduler. This allows you to create and manage all your automated – Cron Jobs – tasks within the framework. If you do not write Cron jobs often, going through documentation or memorizing the Crontab syntax and expression can be extra work (I usually use crontab.guru to simplify this ). Laravel uses functions such as everyTwoMinutes() which is simple to remember and implement. There is also per second scheduling which is not available by default on crontab.

First, you have to open the app/Console/Kernel.php file. Your scheduled task will be added in the schedule(Schedule $schedule) function, by calling the $schedule object. There four main types of calls you can make using the $schedule object:

  1. A closure function containing Laravel/PHP code you wish to run.
  2. A command which runs an Artisan command.
  3. A queued Job.
  4. Command line scripts which can run external scripts.

I will be running an external script (using exec ) to get my systems stats and then send them to myself every 5 minutes. To use an already setup docker environment you can clone this repo and follow the setup instructions (If you choose to use the docker environment. Run your commands in the ‘web’ container) . Copy the code block below in the schedule function:

 
$command = 'echo "------------------" >> out.txt &&';
        $command .= 'top -b -n 1 >> out.txt &&';
        $command .= 'echo "------------------" >> out.txt &&';
        $command .= 'cat /proc/meminfo >> out.txt &&';
        $command .= 'echo "------------------"';
        
        $schedule->exec($command)
        ->appendOutputTo('out.txt')
            ->name('System Stats')
            ->everyFiveMinutes()
            ->emailOutputTo('me@example.com');

Then first test using php artisan schedule:test select the number associated with this task and click enter. if there are no errors, run php artisan schedule:work to start the scheduler if you are on a development environment. For production, add this to your Crontab :

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

NOTE:

  1. Ensure email sender (mailer) of choice is configured on the .env file.
  2. If you choose to use the docker environment. Run your commands in the ‘web’ container.

That’s it, you can include other server stats you want to send through email in the exec function of the scheduler.

References:

  1. Laravel Documentation: https://laravel.com/docs/10.x/scheduling#task-output

Improving systems performance with checksums: Part 1

Checksums are blocks of data generated from another block of data you want to store or transfer to ensure that correct data is transmitted during the process of moving data around. This is useful for data correction and security purposes. For example you could use a checksum algorithm to check if the data you download is the correct one ensuring that you do not download malicious data. Checksums can be implemented using hashes, cryptography, randomization, parity bits etc. 

Another application of checksum if validating data before submitting them to the database. An example of this is when you try to submit your credit card details and any error is flagged before submission to the database. A checksum algorithm checks the validity and correctness of the credit card number you are entering, this prevent the application from having to do a search for incorrect numbers therefore reducing the load the system has to bear.

A checksum algorithm that stood out for me is Luhn’s Algorithm, and I will be making a PHP implementation of it below:

<?php
// Script to check if a number is valid using Luhn's Algorithm

$cardnumber = "4890486808";

$sum = 0;
$numlength = strlen($cardnumber);
$parity = $numlength%2 ;
for($i=0;$i<$numlength-1;$i++){
    if($i%2 != $parity)
        $sum=$sum + $cardnumber[$i];
    elseif ($cardnumber[$i] > 4)
        $sum=$sum+ 2*$cardnumber[$i] -9;
    else
        $sum=$sum+ 2*$cardnumber[$i];
}
echo (10 - ($sum % 10))." is the checksum digit \n";
echo $cardnumber[$numlength-1] == (10 - ($sum % 10)) ? "Correct Number":"Incorrect Number" ;


?>

In the second part, I will be testing this algorithm with a database on Docker and comparing it with an application without a checksum.

Making Custom Numbered List in CSS : Part 1

Creating a custom numbered list in CSS is quite simple. This can be done using a list or any block level element. We will be using the following CSS properties:

  1. counter-reset : To initialize our counter in the parent element.
  2. counter-increment : To specify our increment value for our counter.
  3. content : To output the numbers by replacing elements in the ::before or ::after Selector.

Below is a sample using counter function with block level elements.

See the Pen Custom List in CSS by Sheun (@sheunl) on CodePen.

Custom numbering is useful if you want to create complex numbering systems that are not readily available using <ol> element. or if you want to automatically add special characters to your numbering like brackets. Example below adds nested numbering to a list, using the counters function in this.

See the Pen Custom List in CSS 2 by Sheun (@sheunl) on CodePen.

Hope this provides a quick intro into custom numbered lists in CSS. Watch out for the second part.