Archive for November, 2009

PHP: Password generator from simple to complex, free code

Thursday, November 26th, 2009

Password generation can be a hassle at time. Being human, we like to take the easy way out and stay with the known. However, when it comes to passwords you need to venture into the unknown. These are some neat, little scripts to help you generate random passwords.

Simply copy and paste the code into a file with a “.php” extension. Encase the code in an open and closed PHP environment code ?>To run the file type: “php file.php” return. If you have PHP installed on your computer is will generate the passwords.

This first snippet is the simplest generator. By setting the password length variable you will obtain a random set of numbers for a password to that length.


while ($passlength){
$mystring = mt_rand(0,9);
$mypasswd = $mypasswd . $mystring;
echo $mypasswd . "

This snippet is for generating random letters, both upper and lower case.

while ($passlength){
$letter_array = array("a","b","c","d","e","f","g", "h",
$myletterstring = $letter_array[mt_rand(0,51)];
$mypassword = $mypassword . $myletterstring;
echo $mypassword . "

This snippet is to generate upper and lower case letters and numbers. Since the array item number is randomly generated, you can mix the array any way you want. Remember, the key here is password length. The longer the stronger.

while ($passlength){
$letter_number_array = array("a","b","c","d","e","f","g",
"X","Y","Z", "0","1","2","3","4","5","6","7","8","9");
$myletterandnumberstring = $letter_number_array[mt_rand(0,61)];
$mypassword1 = $mypassword1 . $myletterandnumberstring;
echo $mypassword1 . " 

Here is the final way to generate a random password using the PHP shuffle function.

while ($passlength){
$numbers = array("a","b","c","d","e","f","g", "h","i","j","k","l","m","n","o","p","q","r","s","t","u",
"X","Y","Z", "0","1","2","3","4","5","6","7","8","9");
$myshuffle = $numbers[$passlength];
$mynewpassword = $mynewpassword . $myshuffle;
echo $mynewpassword . "

Remember, when dealing with an array the first item is 0. That means you need to subtract 1 from the numerical item count. You can also add symbols to the array; however, many applications will not take symbols due to the password encryption method.

To see how random the numbers generated are, copy and paste all three snippets in the same PHP file.

Hope this helps and have fun making your passwords harder to guess.

Nagios: service and host escalations made simple

Friday, November 6th, 2009

You are asked to escalate a down host or service to either another technical level or add an incident to your Change Management System. What do you do? Nagios has a great object attribute called escalations for you to configure for the purpose of escalating host and or service issues.

If you already have an existing Nagios host and service monitoring and notification system established, you will be up and running in two steps:

  1. Add any new contacts and or contact groups.
  2. Add the escalation configuration.

I will explain how I added an escalation to notify a Change Management System on the first notification. Once the Change Request is sent you will no longer have to create any more problem tickets, since the technician working the problem should do two things: Acknowledge the problem through Nagios and work the problem ticket.

  1. Add any new contacts for the escalation. I had to add a contact for the Change Management System, since we are able to open an incident using email.
  2. name-of-your-contact-file.cfg

    define contactgroup{
    	contactgroup_name		name of your cms
    	alias				Name of Your CMS
    	members				name_of_your_cms
    define contact{
            contact_name                    name_of_your_cms
            alias                           Name_of_Your_CMS
    	contact_groups			name_of_your_cms
            host_notifications_enabled      1
            service_notifications_enabled   1
            service_notification_period     24x7
            host_notification_period        24x7
            service_notification_options    c #We only need a problem ticket open when the service is critical
            host_notification_options       d #We only need a problem ticket open when the host is down
            service_notification_commands   notify-linux-service-by-email
            host_notification_commands      notify-linux-host-by-email
            can_submit_commands             1
  3. Add the escalation.
  4. Name_of_your_escalation.cfg

     define hostescalation{
     	hostgroup_name		name_of_your_hostgroup
     	first_notification	1
     	last_notification	1
     	notification_interval	5
     	contact_groups		name_of_your_cms_group
    	escalation_period	timeperiod_to_notify_create_incident #ex. 24x7
    	escalation_options      d #We want a problem ticket created when the host is down.

Now, you need to remember how a service escalation works. The escalation is read into Nagios during the reload. When the escalation definition completes, Nagios is smart enough to start with the notification attributes defined in your host and or service template. So, in this instance Nagios will perform the escalation definition and notify your Change Management System once (1). Nagios will continue notifications based upon your notification definitions within the template.

Like everything in life there is a catch. If you create an escalation with a contact group defined within your template, Nagios will only execute the escalation file. For example, if you have a tech_email contact group in the host or service definition and you add it to the CMS contact group within the escalation template both groups will only be notified once (1) when a host or service is down or critical.

That is all there is to creating an escalation for a single purpose. Now, reload Nagios and the escalation will take effect. For service escalations you will be adding a service_definition. Play around with your escalations until you have the correct combination of attributes.

I recommend you add your email address to both groups and reload Nagios. Then select the Notification item in your web-based application. The notification section will show you the contact groups being notified. What I do is add a boggus host definition, add the host group to the escalations file, then reload Nagios.

Have fun and leave a comment if you have any questions or other suggestions for host and service escalations in Nagios.