AppBeat Blog

Perfect Monitoring for Your Cloud

How to detect if Azure SQL database is in primary or secondary role?

If you want to have highly available Azure SQL Database it is highly recommended that you configure Active Geo-Replication (this feature is available for all databases in all service tiers).

Active Geo-Replication consists of two (or more) databases in different regions, where one database is primary (read + write operations) and all other databases are secondaries (allowed only read operations). Secondary databases are automatically synced with changes in your primary database.

Failover event can be triggered manually or if disastrous event happens in one of Azure datacenters. In this case your primary database goes into secondary mode and one of your secondaries becomes new primary database. If you are accessing your databases directly by using connection string, you will have to update it to reflect primary / secondary database switch.

Of course, we want to update our connection string automatically and we will show you two C# code snippets how to detect which database is currently primary (in our examples we operate with one primary and one secondary database).

Example 1

Run special query on each database to determine if it is in primary or secondary role:

public static async Task<string> GetPrimaryConnectionString()
{
	bool? isPrimary = await IsPrimaryAsync(YOUR_CONNECTION_STRING_DB1);
	if (isPrimary == true)
	{
		return YOUR_CONNECTION_STRING_DB1;
	}
	
	isPrimary = await IsPrimaryAsync(YOUR_CONNECTION_STRING_DB2);
	if (isPrimary == true)
	{
		return YOUR_CONNECTION_STRING_DB2;
	}
	
	//could not determine which database is primary
	return null;
}

private static async Task<bool?> IsPrimaryAsync(string connectionString)
{
	try
	{
		using (var conn = new SqlConnection(connectionString))
		{
			if (conn.State != System.Data.ConnectionState.Open)
			{
				await conn.OpenAsync();
			}

			using (var cmd = conn.CreateCommand())
			{
				cmd.CommandText = "SELECT role FROM sys.dm_geo_replication_link_status";
				var res = await cmd.ExecuteScalarAsync();
				return Convert.ToInt32(res) == 0;
			}
		}
	}
	catch (Exception ex)
	{
		//handle exception
		return null;
	}
}

Example 2

If you try to update database which is currently in secondary role you will get following SqlException: Failed to update database "YOUR_DB_NAME" because the database is read-only.

You can use this information to update your connection string:

try
{
	//your code
}
catch (SqlException sqlException)
{
	if (IsReadOnlyDatabase(sqlException) == true)
	{
		//current connection string probably points to secondary database (sqlException.Server)
		//update your connection string to your other database
	}
}

private static bool? IsReadOnlyDatabase(SqlException se)
{
	if (se?.Errors == null) return null;

	foreach (SqlError error in se.Errors)
	{
		if (error.Number == 3906)
		{
			return true;
		}
	}

	return false;
}

 

How to send service outage alerts to your Slack channel?

Want to integrate your website monitoring notifications with Slack? Please follow these instructions.

Step 1

From AppBeat dashboard, click on Account in the left navigation and then choose Integrations from tab menu.

Step 2

Press the Slack button which opens Add notification channel dialog. Enter arbitrary name for this channel and your Slack webhook URL.

To make sure that everything works please click Test button which should send test notification to your Slack channel. If you don't receive any message we suggest that you double check your webhook URL. When everything looks OK you can click Add button.

Step 3

Click on Services & Checks in the left navigation and double click on service for which you want to enable Slack notifications. In Edit service dialog, please click on Notification types tab. Enable your slack channel and click Save. You can repeat this step for your other services.

 

Backup web client for high availability

Some of our users are constantly using our "Live status" page by their monitoring teams. Primarily for this reason we are introducing secondary backup web client which can be used to monitor "Live status" page even if there is issue or outage of primary web client. Backup web client is actively running on different datacenter and can be accessed by navigating to web2.appbeat.io (this is also displayed in application footer).

Is this feature available for our free users? Of course, this feature is available for all our users! If you are not yet member, you can register here for free.

We are one of rare monitoring provider to offer such feature to their users!

---

High level diagram of current AppBeat architecture:

Embed service status to your dashboard

Today we published new version of AppBeat (1.3.4) which allows users to enable embeddable status images for each service. You can access this feature from "Public status / Embed Status":

Images are periodically updated with latest statuses by following rules:

  • if there is no status change for your service then image is refreshed every 5 minutes
  • if service status changes (from good to error, from warning to good, etc.) then we try to upload new status image as soon as possible (this is usually less than 30 seconds after you received your notification)

Status images can be embedded to your internal dashboards or internet facing websites. Image URL contains random Globally Unique Identifier (GUID) in its name which allows you too keep links private / for internal use only.

Optionally you can also customize image template with your own colors and texts (for example, you can remove service name from image).

This feature is available only for paid accounts (from 1.99 EUR / month).

AppBeat web app can now run on Linux!

We have successfully migrated our web application from .NET Core RC1 to .NET Core RC2 and published it on test Linux instance (CentOS). Best of all: AppBeat binaries are 100% identical to those on Windows (same cross platform codebase!) and were simply copied to Linux... And it works! It works great!

Here are some setup details for those who are interested.

First, you have to install .NET Core runtime by following simple instructions on Microsoft .NET Core website.

ASP.NET Core web applications are running on lightweight web server called Kestrel, but since it lacks some of important web server features (for example HTTPS, various built-in tools, ...) it is usually configured to run in tandem with other web server of your choice. On Windows this is IIS, on CentOS we selected NGINX. Those web servers then forward your request to Kestrel which runs your ASP.NET Core web application, creates output and returns it back to main web server.

To setup NGINX to forward requests to Kestrel you have to edit nginx.conf with following (adapt if needed):

location / 
{
proxy_pass http://127.0.0.1:5000;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Original-Proto $scheme;
proxy_set_header X-Original-For $remote_addr;
proxy_http_version 1.1;
}

Then we installed supervisor to monitor our Kestrel process and make sure it is always running:
sudo yum install supervisor

We created kestrel_default.ini file in /etc/supervisord.d/kestrel_default.ini with following content:

[program:kestrel_default]
command=dotnet /usr/share/nginx/WebApp/AppBeat.Web.dll
directory=/usr/share/nginx/WebApp
autorestart=true
autostart=true
stdout_logfile=/var/log/AppBeat/app_std_out.log
stderr_logfile=/var/log/AppBeat/app_err.log

After you saved file you can start Kestrel process by typing:

sudo supervisorctl start kestrel_default

Make sure nginx and supervisor are started on system boot:

sudo systemctl enable nginx
sudo systemctl enable supervisord

This is pretty much it :) We are now prepared for future! If you have questions you can reach us on @AppBeatMonitor

Monitor websites with ease

If your technical support is using AppBeat Live status screen to monitor current performance and availability of your systems, we have new useful feature for you.

You can now enable sound alarm which is automatically triggered when "Live status" icon turns red. This additionally minimizes chance for missed alarms (other notifications, such as Slack, PagerDuty, email, SMS are of course also dispatched as configured).

You can enable / disable this feature by clicking bell icon as shown below:

Before relying on this functionality please click "sound test" link to see if your web browser supports Audio API. Most modern browsers should support this feature out of the box. If everything works you should hear test alarm for approximately 15 seconds.

Would you like to see this feature in action? Sign up for free account and try it yourself!

Integration with PagerDuty - The Incident Resolution Platform

Today we published AppBeat 1.3.0 which implements support for third-party notification providers (from Account / Integrations). We are also happy to announce that first provider to be implemented in AppBeat is PagerDuty (available to all users, even those on Free Plan).

You can add PagerDuty to your AppBeat account by following these easy steps:

  • click "Account / Integrations":

  • click on "PagerDuty" button to add new notification channel (you can add multiple PagerDuty notification types; ServiceKey can be obtained on your PagerDuty website):

  • once you have tested and added your PagerDuty notification please click on "Services & Checks". Now double-click on desired service and click "Notification types" tab. Select your PagerDuty notification:

Congratulations! You have successfully integrated your PagerDuty service! From now on, when your service fails, you will automatically receive incident report directly to your PagerDuty!

We will soon add support for more third party incident management tools. Stay tuned!

Tips & Tricks: bulk importing your website monitors

Few customers who previously used competitive solution asked us if we can help them migrate their web site monitors to AppBeat. We helped them of course and in this process we also created internal tool which speeds up this migration.

If you have too many websites to import them manually, please just contact us and we will gladly assist you. Why bother with manual work if we can do it automatically!

Schedule new probe

If you have less frequent monitoring and you want to check if your website or service still has errors (and you can't wait until next scheduled run), you can click on "schedule new probe" link as shown below:

This will add your monitoring request to scheduler que, and it will be automatically retested as soon as possible (usually this is in less than 10 seconds, but please note that page auto refreshes every 60 seconds - you may also manually refresh page for quicker result).

Don't have AppBeat account yet? Sign up for Free.

Ensuring that correct production version of website is deployed

It is common for web developers to have local "debug" version of website and production version.

For example, in debug version you may have uncompressed, full version of JavaScript files for easier debugging and diagnostics, while in production environment you often deploy smaller and thus faster versions of same files.

To automatically detect situation where you mistakenly published debug version into production environment, you can use free AppBeat account to monitor your web page and alert you of such mistakes. Here is short tutorial how to do this.

Once you are logged-in, click on "Add new check" button which opens "New check" dialog.

On first step simply enter name of your monitor. It is nice to have good name because it will be included in SMS / email notifications:

On second step you enter URL of your page and then click on "Warning" or "Error" tab. Now enter rule similar to this:

In our example we entered warning rule:

NOT(MATCH(%RESPONSE%, "https://appbeat.io/assets/js/AppBeat.min.js"))

When web page from provided URL is downloaded (in our example https://my.appbeat.io/manage/) it is processed first by Error rule, then by Warning rule and finally by Good rule (this is how rules are prioritized). If Error or Warning rules are evaluated to TRUE, notification alert is immediately triggered.

In contrast to Error/Warning rules, Good rule must evaluate to TRUE to be treated as OK. If FALSE is returned it means check is not good and alert is immediately triggered.

If we return again to our rule example:

NOT(MATCH(%RESPONSE%, "https://appbeat.io/assets/js/AppBeat.min.js"))

It simply says that if HTML response from our web server does not contain string https://appbeat.io/assets/js/AppBeat.min.js it triggers Warning signal and then we are notified about this by SMS or email.

If our web site contains correct (minified) version of AppBeat script (AppBeat.min.js), this rule evaluates to FALSE, which means there is no warning. On the other hand, if we mistakenly publish HTML file which contains link to AppBeat.js, rule would evaluate to TRUE and trigger warning.

This is one simple example on how to effectively use AppBeat in real life. Hope it helps you too!