Archive for the ‘ Script ’ Category

Flot Example: Format data in Flot readable JSON

I recently started learning the flot library.  Unfortunately, there aren’t any good examples of how to format the data with JSON in a flot friendly manner.  Below is some basic code that should retrieve data from a database, format it and then JSON encode it. I’ve written the example mostly in psuedo-code, most beginners should be able to pick up the key points of the following example.

 

The script below simply returns JSON, you should create this script in a web accessible directory:

getDataforFlot.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
$mergedData = array();
 
//Get the first set of data you want to graph from the database
$databaseData1 = someFunctionToGetDataFromDatabase($id);
 
//loop through the first set of data and pull out the values we want, then format
foreach($databaseData1 as $r)
{
    $x = $r['x_value'];
    $y = $r['y_value'];
    $data1[] = array ($x, $y);
}
 
//send our data values to $mergedData, add in your custom label and color
$mergedData[] =  array('label' => "Data 1" , 'data' => $data1, 'color' => '#6bcadb');
 
//Get the second set of data you want to graph from the database
$databaseData2 = someFunctionToGetDataFromDatabase($id);
 
 
foreach($databaseData2 as $r)
{
    $x = $r['x_value'];
    $y = $r['y_value'];
    $data2[] = array ($x, $y);
}
 
//send our data values to $mergedData, add in your custom label and color
$mergedData[] = array('label' => "Data 2" , 'data' => $data2, 'color' => '#6db000');
 
 
//now we can JSON encode our data
echo json_encode($mergedData);
?>

Next, just put the following JQuery into your page to render the data using AJAX:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$(document).ready(function(){
 
    $.ajax({
                // usually, we'll just call the same URL, a script
                // connected to a database, but in this case we only
                // have static example files so we need to modify the
                // URL
                url: "/getDataforFlot.php",
                method: 'GET',
                dataType: 'json',
                success: onOutboundReceived
            });
 
    function onOutboundReceived(series) {
        var length = series.length;
        var finalData = series;
        var options = {
            lines: { show: true },
            points: { show: true, hoverable:true },
            grid: { hoverable: true, clickable: true }
        };
        $.plot($("#YOUR-DIV-ID-HERE), finalData, options);
    }
});

Dump Asterisk Realtime Voicemail users into Voicemail.conf

While I’ve been lazy about updating my site, I have not been lazy with writing new scripts and coming up with ideas to write about.

The first in the set that I’m going to make public is a script to dump your Asterisk Realtime Voicemail database users into a flat voicemail.conf file. I used this technique while troubleshooting to revert back to the standard setup while I was working on the realtime config.

#!/bin/bash
function get_mailbox {
 
   mailbox=$( mysql -u dbuser --password=dbpass asterisk -e "select mailbox from voicemail_users where uniqueid = '$i'")
   mailbox=`echo $mailbox | awk '{print $2}'`
 
}
 
function get_password {
 
    password=$(mysql -u dbuser --password=dbpass asterisk -e "select password from voicemail_users where uniqueid = '$i'")
    password=`echo $password | awk '{print $2}'`
 
}
 
function get_fullname {
 
    name=$(mysql -u dbuser --password=dbpass asterisk -e "select fullname from voicemail_users where uniqueid = '$i'")
    name=`echo $name | awk '{print $2}'`
 
}
 
function get_email {
 
    email=$(mysql -u dbuser --password=dbpass asterisk -e "select email from voicemail_users where uniqueid = '$i'")
    email=`echo $email | awk '{print $2}'`
 
}
 
function write_file {
 
    echo "$mailbox => $password,$name,$email,,attach=yes|saycid=no|envelope=no|delete=no" >> /root/test_dump.txt
 
}
 
for (( i=1; i<=151; i++))
do
get_mailbox
get_password
get_fullname
get_email
write_file
done

Asterisk Voicemail Check Disk Usage Script

This latest script was written to prevent 3:00am phone calls. I was recently called because our voicemail server was beginning to run out of disk space. The following script simply scans the voicemail directory, if a user is over 100MB it looks up their extension in the asterisk database and then sends them an email notification to clean it out. Below is the script:

#!/bin/bash
 
#set the maximum disk usage for a user to approx 100MB
size_limit=100000
 
function send_email () {
 
    subject="Voicemail Over Limit"
    message="/tmp/emailmessage.txt.$f"
 
    echo "Hello $name, " > $message
    echo " " >> $message
    echo "This is a notification that your voicemail is currently over the allowed limit.  You are currently using $humansize.  Please either manually delete the voicemails, or you can send an email to patb@codero.com for help with wiping all of the new and saved voicemails in your mailbox." >> $message
    echo " " >> $message
    echo "Thank you," >> $message
    echo "Admin" >> $message
 
    /bin/mail -s "$subject" "$email" < $message
}
 
for f in $(ls /var/spool/asterisk/voicemail/default/)
do
  size=$(du -s /var/spool/asterisk/voicemail/default/$f | awk '{print $1}')
  if [ "$size" -gt "$size_limit" ]
    then
 
        humansize=$(du -sh /var/spool/asterisk/voicemail/default/$f | awk '{print $1}')
        #Query the database for the users email address
        email=$(mysql -u asterisk --password=dbpassword asterisk -e "select email from voicemail_users where mailbox = '$f'")
        #Parse just the email address out of the above query
        email=$(echo $email | awk '{print $2}' )
 
        name=$(mysql -u asterisk --password=dbpassword asterisk -e "select fullname from voicemail_users where mailbox = '$f'")
        name=$(echo $name | awk '{print $2,$3}' )
        #echo "$name    $f  $email  $humansize"
        send_email $email $name $humansize
  fi
    # take action on each file. $f store current file name
done

Rick Roll Script is Back!!! Well sorta…

Notice I used 3 exclamations in the title to convey how excited I am. I’ve been working on developing a new platform that I can better monitor for the Rick Roll Script. I’ve setup a new domain called prankforce.com which now hosts the previous script. I’ve spent a lot of time cleaning this bad boy up for production use and I’ve learned quite a bit in the process. After proving to be one of the most popular features on the site, I think that these changes will be welcomed. If you have any suggestions or questions regarding this project, please let me know.

Right now the site has the following features:

  • custom invite script
  • call logging
  • 1 hour wait between prank calls
  • blacklist of phone numbers that can’t be dialed

Coming features:

  • Message Board
  • ability to suggest new audio and vote for other suggestions (reddit style)
  • a decent UI (Sorry I’m not a designer)

Right now the site is by invite only. If want an invite, just send an email to admin@prankforce.com

Debian/Ubuntu Anti-Virus and Root Kit Scan Script

A customer requested this script, once I started working on it I realized how useful this could be. I have this setup on a weekly CRON task. The script is very simple, it updates ClamAV and RKHunter then scans the server with both and mails the results to the specified email address. If you plan on adding this script to your server, you might want to ensure that you have the correct versions of ClamAV and RKHunter.

Click here to download the script

Click Here to see the full post with install instructions

Open Windows Passive Ports With One Command

A friend of mine just sent me the following command to open passive ports on a Windows Machine. This is the easiest way to open Passive on a Windows I’ve seen.   I’ve run across everything from one liners that loop through every passive port, to full 10 page guides on the topic.  This is hands down the easiest way to open passive ports on a windows firewall I’ve come across.

NOTE: This only works on Windows Server 2008:

Simply issue the following command:

netsh advfirewall set global StatefulFtp enable

Debian/Apache2 Virtualized Host Script

I wrote this script so that I could quickly create virtualized subdomains on my server. I wrote it so that all you have to do to reuse it is simply change the first 3 lines of the file to match your server. This is version 1.o and I plan to add some more error handling and improvements further down the road. Feel free to contact me with any feedback or suggestions.

Click Here to see the full Post
or
Click Here to download the script

Update/New Guide: Lava Lamp Housing

I finished up my hardware arduino monitoring project with a brand new Lava Lamp Housing. I’ve also update the script to check my gmail account and notify me when a new email hits my inbox.

Click Here for the full guide

Arduino Server/Service Monitor

I’ve successfully written a bash script that runs once every minute. The script detects whether or not my server is online by issuing a simple ping request. Based on results of the ping request(s), the arduino will display a green LED when the server is online or a red LED if the server is offline. I’ve also setup a cron job to execute the script once a minute, giving me up to the minute status of my server. This specific project is not terribly amazing, but the foundation for many more projects to come has laid.

Click Here for the full project page

Plesk Hack: Email and FTP Usernames and Passwords

I wrote this simple script for work. This script allows us to gather all the username and password information on the server to let us troubleshoot faster. This is an early version of the script, I plan on added more functionality to improve the speed of troubleshooting. This script was not intended for malicious purposes, please do not use it for such.

Edit: It looks like a few people out there have been publishing my code without attribution.  I really don’t mind if you post my code, but please give me credit.  The one I’ve found didn’t even change the header (with my full name in it) of the script or the “doneskies” at the end.  Have some class and give me proper attribution.

Click Here to see the script