Website monitoring with python and Salesforce

\"webup\"
 

In this post we’ll explore how you can implement a very simple website alive check using python(urllib2) and storing your status/results in Salesforce(simple_salesforce). There are many applications that can provide this service but I fancied a bit of fun. And love the idea of a dashboard in Salesforce to display another metric “100% uptime for web services”.

What you’ll need:

  1. Salesforce Username/Password/Security Token (signup for Developer org here).
  2. Python installed on your local computer with pip working (python windows / osx / linux – pip comes with Python 2.7x+).
  3. Amazon AWS Account (sign up here).
  4. Putty / Puttygen (download them here – this is used for getting access to a remote server and used in the AWS tutorial if your on windows)
  5. A little knowledge of Creating Objects/Fields in Salesforce – if not head over to Trailhead first and complete this Project (Build a Battle Station – you’ll love it but don’t forget to come back).
  6. A little knowledge of Python – not essential the script I’m sharing just works but you may want to expand it.
  7. An understanding of CRON Jobs (read here)

Now we have the formalities out the way lets get cracking.

Setup Salesforce

  1. Login to Salesforce you need to create 2 objects for storing data in
  2. Object 1
    1. Label = Website
    2. Pluaral Label = Websites
    3. Object Name = Website
    4. Description = An object for storing website names and urls
    5. Record Name = Website Name
    6. Allow Reports = TRUE
    7. Launch New Custom Tab Wizard after saving this custom object = true
    8. Save
    9. Now you need to add 2 custom fields
      1. Name = URL / API Name = URL__c / Data type = URL(255) / Required = TRUE / Description = ‘Website URL for tracking’
      2. Name = Active / API Name = Active__c / Data type = Checkbox / Default = TRUE / Description = ‘A way of deactivating a website from being checked’
    10. Object 2
      1. Label = Website Status
      2. Pluaral Label = Website Status
      3. Object Name = Website_Status
      4. Description = An object for storing your status updates for each website
      5. Allow Reports = TRUE
      6. Save
      7. Now you need to add a few custom fields
        1. Name = Status / API Name = Status__c / Data type = Checkbox / Default = FALSE / Description = ‘A confirmation flag for confirming that the site was alive’
        2. Name = Status Message / API Name = Status_Message__c / Data type = TEXT(255) / Description = ‘Storing a status message mainly for errors’
        3. Name = Month / API Name = ‘Month__c / Data type = Formula(text) / Description = ‘Displaying the month that entry was created for matrix reporting) / Formula = CASE(MONTH( datevalue(CreatedDate) ), 1, “January”, 2, “February”, 3, “March”, 4, “April”, 5, “May”, 6, “June”, 7, “July”, 8, “August”, 9, “September”, 10, “October”, 11, “November”, 12, “December”, “None”)
        4. Name = Year / API Name = ‘Year__c’ / Data type = Formula(text) / Description = ‘The year the status was added to salesforce’ / Formula = text(year(datevalue(CreatedDate)))
        5. Crate a lookup relationship field with website object, Master_Website__c.

At this point you have created the basics for storing records in your org, now head to your Website tab and add a website URL and mark it as active.

Now you could create some reports (these records can display nicely in a matrix report with a chart if you add a Uptime(%) formula to divide sum of status / record count) but there isn’t any data in there yet so it’ll look boring. Create some data and check once how it looks like.

Python Script

Now for the fun bit first up we need to prepare our local environment so that we can be sure before copying onto our remote environment that it’s all working.

  1. Open Terminal or Command Prompt on your computer.
  2. If your familiar with virtualenv then setup and activate it.
  3. Now your ready to install simple_salesforce, type:
    pip install simple_salesforce
    

    .

  4. If your python environment is setup correctly that should be it your now ready to build out your script.
  5. As with other guides I’ve tried to comment the code in english so you can copy/paste and follow what each line is doing. Using an editor like Sublime create a file or use pycharm editor as per your favored (viz. programmer) choice.
    #!/usr/bin/python
    # import urllib2 - https://docs.python.org/2/library/urllib2.html
    import urllib2
    # import simple salesfore from simple_salesforce - https://pypi.python.org/pypi/simple-salesforce
    from simple_salesforce import Salesforce
    # setup your login details
    salesforce_login = {\'username\':\'yourusername\', \'password\':\'yourpassword\', \'security_token\':\'yourkey\'}
    # now connect to Salesforce
    sf = Salesforce(**salesforce_login)
    # now setup to loop through the records stores in the website
    for row in sf.query(\"SELECT Id, Name, URL__c FROM Website__c WHERE Active__c=true\")[\"records\"]:
        # assign the sfid for this row to ensure that when you populate the Website_Status__c object it\'s linked to the correct website
        sfid = row[\"Id\"]
        # try and see if you can connect
        try:
            # define the actual url for th test
            websiteurl = row[\"URL__c\"]
            # use urllib2 to try and open the website
            urllib2.urlopen(websiteurl)
            # because this all went well we\'re going to populate the Website_Status__c with a Active Confirmation
            sf.Website_Status__c.create({\'Master_Website__c\': sfid,\'Status__c\': 1,\'Status_Message__c\':\'Site Up\'})
        # catch a HTTPError like 401/500/303 and log it
        except urllib2.HTTPError, e:
            # get the error details for storing in salesforce
            errorcode = e.code
            # unlike earlier we\'ll put an error status in with the code
            sf.Website_Status__c.create({\'Master_Website__c\': sfid,\'Status__c\': 0,\'Status_Message__c\':errorcode})
        # catch wnen a bum url is stored in your salesforce and error out lile htp://ww.jondo.com - error url
        except urllib2.URLError, e:
            # get the error details for storing in salesforce
            errorcode = e.args
            # again lets put in an error status for reference later
            sf.Website_Status__c.create({\'Master_Website__c\': sfid,\'Status__c\': 0,\'Status_Message__c\':errorcode})

    .

  6. Now save this file as websitechecker.py

So we have the file, lets now doing a quick test, from your terminal.

python websitechecker.py

That’s it now just so long as you have followed the instruction here, go to Salesforce and open your Website, you should see a related record with a status attached.

\"website-status.png\"

I’ve run it a few times so I could play about with the reports I mentioned earlier.

Amazon EC2 Setup

Instead of me taking you through every step I thought I’d just share a link to Amazon’s tutorial seeing as I’m sure this will slightly change over the years but this script and Salesforce won’t change that much.

  1. Follow this Guide and setup with the following:
    1. Image Type = Amazon Linux AMI.
    2. Instance Type = t2.micro (free tier elligible).
    3. Take all defaults until security group setup.
      1. Give your group a name and description.
      2. Set SSH to MyIP – this will ensure you can access the server.\"securitysetup\"
  2. At this point I would hope that you have a putty session running and are at a command line.
  3. Now type in the follow commands to setup the instance:
    1. sudo yum update
      1. wait for it to prompt you then press y
    2. sudo pip install simple_salesforce
    3. nano websitechecker.py
      1. now paste in the python script above
      2. press ctrl+o to save and edit nano
    4. now type in chmod +x websitechecker.py to make your script executable
    5. now type crontab -e
    6. press i – to insert
    7. now type in the following to run this script every 5 mins:
    8. */5 * * * * /home/ec2-user/websitechecker.py
    9. now restart cron
      1. sudo service crond restart

Now site and wait 10 mins and then check in Salesforce to see if your records have been created.

So that’s how you monitor a website in a very simple and crude way using python and salesforce.

Warning – if you add 100’s of URL’s your going to use a lot of API calls for every 5 mins you’re using 1 API just checking for a website. The each site you’re checking is 288 API calls a day. That’s also going to be the same number of records created in your org each day.

Additional ways, ideas for expanding this project

  • Process Builder – Email Notifications/Push Notifications/Chatter Messages/Custom Apex(if twilio installed call send an SMS).
  • Reporting Snapshot – Then you can clear down data and keep the key daily / weekly historical data instead of each record. (keeping down your data volumes)
  • SNMP – (here/)
  • Ping – (here)
  • Website Response Time (here)
  • Login Credentials (here)

Leave a Comment

Your email address will not be published. Required fields are marked *

Select Language »