Showing posts with label scheduler. Show all posts
Showing posts with label scheduler. Show all posts

Monday, May 25, 2015

Azure Scheduler - Job Schedule Starting On value explained

PROBLEM: For me it wasn't clear what value the Azure Scheduler - Job Schedule "Starting On" value was meant to represent (there is a list of times AM and PM and list of UTC offsets). I wasn't sure if I was meant to be entering the GMT (UTC) value for when I wanted my job to run or the local time I wanted the job to run (it's obvious after you do it). (if you just want general info on Azure Scheduler hit this link: http://azure.microsoft.com/en-us/documentation/articles/scheduler-get-started-portal/#create-a-job-collection-and-a-job)

SOLUTION: Short answer, it is the local time. When creating a new scheduled job, the job schedule "Starting On" value is the local date/time you want the job to run, to which you apply the relevant UTC offset based on the time zone of the local area e.g. If I want a job to execute at 8.30pm AEST (Australian Eastern Standard Time i.e. not daylight savings), I'd set the date to the date I want and the time to 8.30 PM and the UTC drop down list to UTC 10:00 (i.e. the time is not a GMT/UTC time, it is the local time zone you're interested in, reflected by the UTC offset value).

When you click Save, Azure will then convert this to a GMT (UTC - go here if you want the technical difference between the two http://www.timeanddate.com/time/gmt-utc-time.html) value e.g. 2015-05-25 8:30 PM UTC 10:00 will display as Mon, 25 May 2015 10:30:00 GMT (you'll no longer see the local time and UTC offset that you originally entered, this confused me a bit when looking at other schedules that others had set up).

NOTE: The big gotcha is that all that is saved is a GMT time, not a time zone - this means you'll potentially need to update your schedule for daylight savings.
http://codeofmatt.com/2013/11/04/windows-azure-scheduler/
https://blogs.endjin.com/2015/04/azure-automation-scheduler-and-daylight-saving-time/

Vote on this improvement/fix here:
http://feedback.azure.com/forums/246290-azure-automation/suggestions/6621981-fix-the-scheduler-to-be-aware-of-daylight-saving

Wednesday, April 11, 2012

Checking Server Disk Space

PROBLEM: You have a large network of computers, you want to know when they are getting low on space since that can cause many issues such as websites running slowly or not at all, logs might not get written, defragging can't occur, windows updates can't be applied etc. So you want to monitor them constantly but don't want to waste time doing it.
SOLUTION: I'm sure there are products out there (free and paid for) to do this but my solution was to go the DIY path and use PowerShell and two Windows Scheduled Tasks (one daily that will email any warnings and one weekly that will email regardless). I have zero PowerShell experience but have borrowed liberally from a few fellows (attributions in the script). I'll give you the code and then raise a few points about it:


##############  Script starts Here ##########

# This script is designed to loop through a list of servers in an external file and check the
# space on each drive associated with each server, report on their free disk space percentage
# and output the results to the screen (if run in a console), and an email (which will only get
# sent if the parameter of WarningsOnly is passed when executing the script
# e.g. C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -command "C:\ServerDiskSpaceChecker\ServerDiskSpaceChecker.ps1 WarningsOnly").
#
# Acknowledgements:
# DISK SPACE PORTION OF THE SCRIPT TAKEN AND MODIFIED FROM: http://www.youdidwhatwithtsql.com/check-disk-space-with-powershell-2/195
# EMAIL PORTION OF THE SCRIPT TAKEN AND MODIFIED FROM: http://www.techrepublic.com/blog/window-on-windows/send-an-email-with-an-attachment-using-powershell/4969
# EMAIL HTML FORMATTING PORTION OF THE SCRIPT TAKEN AND MODIFIED FROM: http://exchangeserverpro.com/powershell-send-html-email
# SCHEDULING PROCESS (no code required but good to know) TAKEN FROM: http://dmitrysotnikov.wordpress.com/2011/02/03/how-to-schedule-a-powershell-script/



# MODIFY THE BELOW VARIABLES AS REQUIRED

# Issue warning if % free disk space is less
$percentWarning = 15;

# Get server list (text file containing server names)
$servers = Get-Content "C:\ServerDiskSpaceChecker\computers.txt";

# IP address of email server
$smtpServer = "10.10.0.999"

# Email address to send notifications to
$emailTo = "ITTeam@yourcompanyhere.com.au"



# LET'S CHECK SOME DISKS!

$warningsExist = $false;
$scriptParameter = $args[0];
$msgSubject = "Server Disk Space Checker Results";
$datetime = Get-Date -Format "yyyyMMddHHmmss";

# variable for storing body of email (formatted in html so that we can use pretty colours)
$emailBody = "<p>The following are the results of an automated check of remaining server disk space (Refer to the <a href='http://yourwikiurlhere'>wiki</a> for more information). Entries will be marked in red if they fall below the current threshold - please investigate these ASAP.<p>";

# Add headers to log file
Add-Content "$Env:USERPROFILE\server disks $datetime.txt" "server,deviceID,size,freespace,percentFree";

foreach($server in $servers)
{
# Get fixed drive info
$disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3";

foreach($disk in $disks)
{
$deviceID = $disk.DeviceID;
[float]$size = $disk.Size;
[float]$freespace = $disk.FreeSpace;

$percentFree = [Math]::Round(($freespace / $size) * 100, 2);
$sizeGB = [Math]::Round($size / 1073741824, 2);
$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);

$colour = "Green";
if($percentFree -lt $percentWarning)
{
$colour = "Red";
$warningsExist = $true;
}

# Get results
$results = "$server $deviceID percentage free space = $percentFree% (Total Size=$sizeGB GB, Total Free=$freeSpaceGB GB)"

# Write results to email
$emailBody = $emailBody + "<span style='color:$colour'>$results</span><br />"

# Write results to screen
Write-Host -ForegroundColor $colour $results;
}
}



# Send an email with the details

if(($warningsExist -eq $true) -or ($scriptParameter -ne "WarningsOnly"))
{
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue;

$msg = new-object Net.Mail.MailMessage;
$smtp = new-object Net.Mail.SmtpClient($smtpServer);
$msg.From = "system@yourcompanynamehere.com.au";
$msg.To.Add($emailTo);
if($warningsExist -eq $true)
{
$msgSubject = "Warnings Exist - " + $msgSubject;
}
$msg.Subject = $msgSubject;
$msg.IsBodyHTML = $true;
$msg.Body = $emailBody;
$smtp.Send($msg);
}


############## End of Script ##########

To enable the PowerShell script to run permissions needed to be changed on the server to allow local scripts to run ok (but external scripts require signing) - run this command in PowerShell:

  • Set-ExecutionPolicy RemoteSigned
There are a number of variables within the script that can be modified:
  • $percentWarning (Issue warning if % free disk space is less)
  • $servers (text file containing server names))
  • $smtpServer (IP address of email server)
  • $emailTo (Email address to send notifications to)
To specify which servers to check, create a file (e.g. computers.txt) and simply have a list of server names, one on each row, no empty rows at the end, and ensure the $servers variable uses that file.

When executing the script, if you want it to only email you when there is a warning then pass the parameter "WarningsOnly" after the script name (provide any other value if you want to always be notified) e.g. C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -command "C:\Server Disk Space Checker\ServerDiskSpaceChecker.ps1 WarningsOnly"

To troubleshoot run the script/scheduled task manually - if you include the -NoExit flag when running the script it will not close the PowerShell window when it's done so you can see any errors etc that may have occurred.
    Common problems include:
    • Incorrect server name in computers.txt
    • Blank lines in computers.txt
    • Incorrect script permissions (refer above)
    • NaN% in the email (this means the script has had a problem reading the disk space - NaN stands for Not a Number - add the -NoExit flag and see what's happening).
    This script is basically a Frankenstein compliation of a few different scripts (refer to the URL references within the script itself) - I'm definitely no PowerShell expert, but it seems to do a good job when combined with the schedule task and parameters. (apologies for the blog formatting - I'm really not a big fan of the Blogger formatting!).

    Friday, January 20, 2012

    Windows Services

    PROBLEM: Recently I created a simple windows service with .net 4 to perform a check periodically on the status of our company's websites (are they up or not) and send an email notification if they aren't.

    SOLUTION: While not particularly challenging there definitely some lessons learnt...
    • Firstly, people (http://weblogs.asp.net/jgalloway/archive/2005/10/24/428303.aspx) will point out that probably creating a console program which can be scheduled with the windows scheduler is the easier and better approach to this problem, but I mainly just wanted the experience in creating a windows service.
    • Some good web service examples:
    • Add a try/catch around your code in Main which logs any exceptions - useful to determine problems if your service won't start correctly etc.
    • To debug a windows service in Visual Studio you need to open the solution and then choose Debug > Attach to Process... > Tick both "Show processes from all users" and "Show processes in all sessions" and then click the exe and choose Attach. Set a breakpoint and away you go (remember to stop the timer while debugging if it is set to raise an event frequently otherwise you won't be able to step through the code easily).
    • When using windows logging, logs are identified by their first 8 characters - if you try and create two different logs, with the same first 8 characters, it will cause an exception.
    • The HttpWebRequest.GetResponse method returns various HttpStatusCode enums BUT quite often will raise an exception (e.g. if the website is down), so make sure you use a try/catch and it is from the exception (cast to WebException) that you can get the HttpStatusCode i.e. WebException.Response.StatusCode).
    • When you create a new log (not a new entry within a log), the windows Event Viewer doesn't do a great job at refreshing and showing it (even when you choose refresh) - just close the Event Viewer and open it again.
    • If you are testing a service and constantly installing and uninstalling it, you may get the system into a state where the install starts failing or complaining that the service has been marked for deletion but can't be removed. Try shutting down the Services window and see if that makes a difference, or try manually uninstalling it view Add/Remove Programs or via installutil /u but usually you'll just resolve it quicker by restarting your computer.
    • If you want to use an app.config with your windows service, you'll need to manually add a reference to the System.Configuration DLL so that you can use ConfigurationManager.AppSettings["BLAH"] (it's not included by default in a windows service project).