Announcement

Collapse
No announcement yet.

Queued Message Alerts

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Queued Message Alerts

    I have worked on a function (that will run in the postprocessor) that will page person on call when queued messages from any deployed channel reaches a set number. It will also stop/start a certain interface when x number of queued messages reach a certain number. The function needs to be a context of channel. You will need to set 2 channel maps one for the channel name and one for the channelid. Then you will need to set a var that has your alert values. I have some extra commands commented out that can be used for future reference. If you see any changes that can make this function better please let me know.

    Thanks,
    Chad

    function MirthAlertsFunction(channelID, channelName, alertValues)
    {
    // Create an instance of the channelStatusController object
    var channelStatusController = Packages.com.webreach.mirth.server.controllers.Cha nnelStatusController.getInstance();

    // Commands that go with ChannelStatusController
    // Start the channel responsible for processing the tables just created
    //channelStatusController.startChannel( channelID );

    // Stop
    //channelStatusController.stopChannel( channelId );

    // Pause
    //channelStatusController.pauseChannel( channelID );

    // Resume
    //channelStatusController.resumeChannel( channelID );


    // Create an instance of the ChannelStatisticsController object

    // getStatistics
    var channelStatisticsController = Packages.com.webreach.mirth.server.controllers.Cha nnelStatisticsController.getInstance();

    // Commands that go with ChannelStatisticsController

    // Get channel Statistics
    var stats = channelStatisticsController.getStatistics( channelID );


    // Create Subject and Body
    var smtpSubject = channelName + " has an issue"
    var smtpBody = channelName + " has been stopped because it has " + stats.queued + " messages Queued";


    // Split alertValues to get the text values
    // QueuedValue,smtpto,smtpcc,smtpfrom
    var alertValues = alertValues.split('^');


    if (stats.queued > alertValues[0])
    {
    if(stats.alerted == '0')
    {
    // send message
    var smtpConn = SMTPConnectionFactory.createSMTPConnection();
    smtpConn.send(alertValues[1], alertValues[2], alertValues[3], smtpSubject, smtpBody);

    // So we do not get multiple pages will will increment the Alerted stats by 1
    channelStatisticsController.incrementAlertedCount( channelID );
    }
    } else {
    if ((stats.queued == alertValues[0] / 2 && stats.queued != '0'))
    {
    // Stop Interface
    channelStatusController.stopChannel( channelID );

    // Start Interface
    channelStatusController.startChannel( channelID );

    } else {
    if (stats.queued < alertValues[0] && stats.alerted >= '1')
    {
    channelStatisticsController.clearStatistics( channelID, false, false, false, false, false, true);
    }
    }
    }
    return;
    }

  • #2
    Thanks! I've being looking for something like this and am glad you posted it.

    Comment


    • #3
      Updated code

      Please let me know what you think of the new code.
      Attached Files

      Comment


      • #4
        Hi Chad,

        This looks really nice. What all do i need to update in this? Just email address? I am anxious to tinker around with this tomorrow.

        Ken
        Last edited by kmickeletto; 03-16-2011, 01:28 PM.

        Comment


        • #5
          You will need to update var AddName, AlertAdmin

          Then you will need to update the case statements with The name of you channel in the switch statements. The default is a catch all, the other case statements are for any special channels you may have. You can add more case statements(for additional channels) as needed. Let me know how it works for you. Feel free to email me if you run into any problems.

          Thanks,
          Chad

          Comment


          • #6
            Needs From email address

            Chad,
            Thanks for the Alert code, works for me!
            I tried it using version 2.2 and found that my email function was looking for a qualified From email address i.e. "[email protected]".
            Thanks again,
            Kevin

            Comment


            • #7
              I also wanted to say thank you for posting this channel - I'm going to use a variation of it.

              I found that as is, it doesn't play nice with the standard Mirth alerts. It checks and modifies each channel's 'alerted' statistic but if you have other Mirth alerts running at the same time then they step on each others toes and you can miss sending these 'messages are queued' emails (if the alerted statistic was already > 0 from another Mirth alert) and you can send a bunch of extra 'the queue is clear now' emails (anytime the queue is < 10 but the alerted stat is > 0 from another Mirth alert).

              I got around this by updating the script to use a global channel map rather than the standard 'alerted' channel statistic.

              Comment


              • #8
                Queue messaged alerts - Mirth 2.2

                Hello, I have taken this example and created an alert as below : It fails to actually Alert at all, and displays this error in the server log : Any help much appreciated! Does it have anything to do with the new version since this post is older? Or, I supposed I am missing something very obvious given that I am a newbie!


                [2012-08-28 14:26:41,252] ERROR (com.mirth.connect.connectors.js.JavaScriptMessage Receiver:145): org.mozilla.javascript.WrappedException: Wrapped org.apache.commons.mail.EmailException: Missing final '@domain' (bfc62f2e-d170-4507-83a9-8cce08baaa71#93)


                [2012-08-28 14:27:37,571] FATAL (org.mule.impl.DefaultComponentExceptionStrategy:8 3): The Default Component Exception Strategy has been invoked but there is no current event on the context

                [2012-08-28 14:27:17,410] FATAL (org.mule.impl.DefaultComponentExceptionStrategy:8 4): The error is: null
                java.lang.NullPointerException
                at com.mirth.connect.connectors.js.JavaScriptMessageR eceiver.poll(JavaScriptMessageReceiver.java:81)
                at org.mule.providers.PollingMessageReceiver.run(Poll ingMessageReceiver.java:97)
                at org.mule.impl.work.WorkerContext.run(WorkerContext .java:290)
                at edu.emory.mathcs.backport.java.util.concurrent.Thr eadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1061)
                at edu.emory.mathcs.backport.java.util.concurrent.Thr eadPoolExecutor$Worker.run(ThreadPoolExecutor.java :575)
                at java.lang.Thread.run(Unknown Source)


                Channel source javascript :

                // Here we will set up the values for the Alert Email/Pages
                // Here we will set the flags for Ugrades or Downtime
                var AlertFlagDowntime = "N";
                var AlertFlagUpgrade = "N";

                // Here we will set values for each user
                var AddName = '[email protected]';

                // Value for Admin
                var AlertAdmin = AddName;


                // First thing we want to see if we need to send alerts based on the alert flags
                if(AlertFlagUpgrade.toString() == "N" && AlertFlagDowntime.toString() == "N")
                {

                // Now we want to check to see if Hour is not 00 or 01. This is when most systems are doing there nightly backup and messages could be queued nightly during
                // this process.
                if(DateUtil.getCurrentDate('HH') >= '02')
                {
                // create an array of deployed channels
                // Create an instance of the channelStatusController object
                var channelStatusController = Packages.com.mirth.connect.server.controllers.Chan nelStatusController.getInstance();
                var test = channelStatusController.getChannelStatusList();

                // get stats of each channel in array
                var dc = channelStatusController.getChannelStatusList().toS tring().split("],");

                for (var i=0; i<dc.length; i++)
                {
                // Now we will get the channelID so we can get the stats.queud
                var getchannellist = dc[i].toString().split("=");
                var getchannelid = getchannellist[1].toString().split(",");
                var channelid = getchannelid[0];

                // Now we will get the stats for that channelid

                // Create an instance of the ChannelStatisticsController object
                // getStatistics
                var channelStatisticsController = Packages.com.mirth.connect.server.controllers.Chan nelStatisticsController.getInstance();

                // Commands that go with ChannelStatisticsController
                // Get channel Statistics
                var stats = channelStatisticsController.getStatistics( channelid );

                // Now we will check to see if the the stats.queued is over 100
                // If so we need to compare the channel Name to get some values to use for the alerts
                if(stats.queued > 5)
                {
                // Now we need to get the Channel Name
                var channelController = Packages.com.mirth.connect.server.controllers.Chan nelController.getInstance();
                var CN = channelController.getDeployedChannelById(channelid ).getName();

                // Now that we have the channel Name we need to see what criteria to use for the alert

                // Now we will make a value to use as a switch variable
                var CNValue = CN.toString();

                switch (CNValue) {
                case 'SORIAN':
                // Now we will see if the stats.queued equals values to send page
                if(stats.queued > '300' && stats.alerted == '0'){
                // send issue message
                var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                var smtpto = AlertAdmin;
                var smtpsubject = CN + " has an issue"
                var smtpbody = CN + " has " + stats.queued + " messages Queued";

                var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                smtpConn.send(smtpto, '', 'Mirth', smtpsubject, smtpbody);

                // So we do not get multiple pages will will increment the Alerted stats by 1
                channelStatisticsController.incrementAlertedCount( channelid );
                }


                break;


                default:
                // Now we will see if the stats.queued equals values to send page
                if(stats.queued > '100' && stats.alerted == '0'){
                // send issue message
                var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                var smtpto = AlertAdmin;
                var smtpsubject = CN + " has an issue"
                var smtpbody = CN + " has " + stats.queued + " messages Queued";

                var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                smtpConn.send(smtpto, '', 'Mirth', smtpsubject, smtpbody);

                // So we do not get multiple pages will will increment the Alerted stats by 1
                channelStatisticsController.incrementAlertedCount( channelid );
                }

                break;
                }
                } else {
                if (stats.queued < 10 && stats.alerted >= '1')
                {
                channelStatisticsController.clearStatistics( channelid, false, false, false, false, false, true);


                // Now we need to get the Channel Name
                var channelController = Packages.com.mirth.connect.server.controllers.Chan nelController.getInstance();
                var CN = channelController.getDeployedChannelById(channelid ).getName();

                // Now that we have the channel Name we need to see what criteria to use for the alert

                // Now we will make a value to use as a switch variable
                var CNValue = CN.toString();

                switch (CNValue) {

                case 'SORIAN':
                // send Resolved message
                var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                var smtpto = AlertAdmin + ", " + AlertConnectRX;
                var smtpsubject = CN + " issue had been resolved"
                var smtpbody = CN + " messages are processing now ";

                var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                smtpConn.send(smtpto, '', 'Mirth', smtpsubject, smtpbody);
                break;

                default:
                // send Resolved message
                var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                var smtpto = AlertAdmin;
                var smtpsubject = CN + " issue had been resolved"
                var smtpbody = CN + " messages are processing now ";

                var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                smtpConn.send(smtpto, '', 'Mirth', smtpsubject, smtpbody);
                break;
                }

                }
                }
                }
                }
                }

                Comment


                • #9
                  smtpConn.send(smtpto, '', 'Mirth', smtpsubject, smtpbody);

                  You now need to have something like:

                  smtpConn.send(smtpto, '', '[email protected]', smtpsubject, smtpbody);

                  Comment


                  • #10
                    Queue Message Alert

                    Chad,
                    That did it.

                    Thanks,
                    Scott

                    Comment


                    • #11
                      I wanted to post a note with an additional tweak I needed to make to this channel to work properly: the switch statement used for the channel-specific overrides is trying to do a string comparison but failing because we're comparing a java string to a javascript string and the switch statement doesn't care for that (I believe). I got around it by updating the statement from this:
                      switch (CNValue) {

                      to this:
                      switch (String(CNValue)) {


                      With all my tweaks, my full script now looks like this:

                      // Here we will set up the values for the Alert Email/Pages
                      // Here we will set the flags for Ugrades or Downtime
                      var AlertFlagDowntime = "N";
                      var AlertFlagUpgrade = "N";

                      // Here we will set values for each user
                      var AddName = '<to email addresses here separated by commas>';

                      // Value for Admin
                      var AlertAdmin = AddName;

                      // Who should emails come from?
                      var smtpfrom = '<from email address here';


                      // First thing we want to see if we need to send alerts based on the alert flags
                      if(AlertFlagUpgrade.toString() == "N" && AlertFlagDowntime.toString() == "N")
                      {

                      // Now we want to check to see if Hour is not 00 or 01. This is when most systems are doing there nightly backup and messages could be queued nightly during
                      // this process.
                      if(DateUtil.getCurrentDate('HH') >= '02')
                      {
                      // create an array of deployed channels
                      // Create an instance of the channelStatusController object
                      var channelStatusController = Packages.com.mirth.connect.server.controllers.Chan nelStatusController.getInstance();
                      var test = channelStatusController.getChannelStatusList();

                      // get stats of each channel in array
                      var dc = channelStatusController.getChannelStatusList().toS tring().split("],");

                      for (var i=0; i<dc.length; i++)
                      {
                      // Now we will get the channelID so we can get the stats.queud
                      var getchannellist = dc[i].toString().split("=");
                      var getchannelid = getchannellist[1].toString().split(",");
                      var channelid = getchannelid[0];

                      // Now we will get the stats for that channelid

                      // Create an instance of the ChannelStatisticsController object
                      // getStatistics
                      var channelStatisticsController = Packages.com.mirth.connect.server.controllers.Chan nelStatisticsController.getInstance();

                      // Commands that go with ChannelStatisticsController
                      // Get channel Statistics
                      var stats = channelStatisticsController.getStatistics( channelid );

                      // Now we need to get the Channel Name
                      var channelController = Packages.com.mirth.connect.server.controllers.Chan nelController.getInstance();
                      var CN = channelController.getDeployedChannelById(channelid ).getName();


                      // Now we will check to see if the channel has fired an alert already
                      // If not we need to compare the channel Name to get some values to use for the alerts
                      if($gc('queuealerted' + channelid) < '1')
                      {

                      // Now that we have the channel Name we need to see what criteria to use for the alert
                      // Now we will make a value to use as a switch variable
                      var CNValue = CN.toString();

                      switch (String(CNValue)) {
                      case '<case sensitive channel name here>':
                      // Now we will see if the stats.queued equals values to send page
                      if(stats.queued > '150'){
                      // send issue message
                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      var smtpto = AlertAdmin;
                      var smtpsubject = 'MIRTH QUEUE ALERT: ' + CN;
                      var smtpbody = CN + " has " + stats.queued + " messages Queued";

                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      smtpConn.send(smtpto, '', smtpfrom, smtpsubject, smtpbody);

                      // So we do not get multiple pages will will increment the Alerted stats by 1
                      // channelStatisticsController.incrementAlertedCount( channelid );
                      $gc('queuealerted' + channelid, '1');
                      }
                      break;

                      case '<case sensitive channel name here>':
                      // Now we will see if the stats.queued equals values to send page
                      if(stats.queued > '0'){
                      // send issue message
                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      var smtpto = AlertAdmin;
                      var smtpsubject = 'MIRTH QUEUE ALERT: ' + CN;
                      var smtpbody = CN + " has " + stats.queued + " messages Queued";

                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      smtpConn.send(smtpto, '', smtpfrom, smtpsubject, smtpbody);

                      // So we do not get multiple pages will will increment the Alerted stats by 1
                      // channelStatisticsController.incrementAlertedCount( channelid );
                      $gc('queuealerted' + channelid, '1');
                      }
                      break;

                      default:
                      // Now we will see if the stats.queued equals values to send page
                      if(stats.queued > '100'){
                      // send issue message
                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      var smtpto = AlertAdmin;
                      var smtpsubject = 'MIRTH QUEUE ALERT: ' + CN;
                      var smtpbody = CN + " has " + stats.queued + " messages Queued";

                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      smtpConn.send(smtpto, '', smtpfrom, smtpsubject, smtpbody);

                      // So we do not get multiple pages will will increment the Alerted stats by 1
                      // channelStatisticsController.incrementAlertedCount( channelid );
                      $gc('queuealerted' + channelid, '1');
                      }
                      break;
                      }

                      //if it has alrady fired an alert, check if it's time to resolve the alert
                      } else {
                      // Now that we have the channel Name we need to see what criteria to use for the alert
                      // Now we will make a value to use as a switch variable
                      var CNValue = CN.toString();

                      switch (String(CNValue)) {

                      case '<case sensitive channel name here>':
                      if (stats.queued < 1)
                      {
                      // send Resolved message
                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      var smtpto = AlertAdmin + ", " + AlertConnectRX;
                      var smtpsubject = 'MIRTH QUEUE RESOLVED: ' + CN;
                      var smtpbody = CN + " messages are processing now ";

                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      smtpConn.send(smtpto, '', smtpfrom, smtpsubject, smtpbody);
                      //channelStatisticsController.clearStatistics( channelid, false, false, false, false, false, true);
                      $gc('queuealerted' + channelid, '0');
                      }
                      break;

                      case '<case sensitive channel name here>':
                      if (stats.queued < 1)
                      {
                      // send Resolved message
                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      var smtpto = AlertAdmin + ", " + AlertConnectRX;
                      var smtpsubject = 'MIRTH QUEUE RESOLVED: ' + CN;
                      var smtpbody = CN + " messages are processing now ";

                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      smtpConn.send(smtpto, '', smtpfrom, smtpsubject, smtpbody);
                      //channelStatisticsController.clearStatistics( channelid, false, false, false, false, false, true);
                      $gc('queuealerted' + channelid, '0');
                      }
                      break;

                      default:
                      if (stats.queued < 10)
                      {
                      // send Resolved message
                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      var smtpto = AlertAdmin;
                      var smtpsubject = 'MIRTH QUEUE RESOLVED: ' + CN;
                      var smtpbody = CN + " messages are processing now ";

                      var smtpConn = SMTPConnectionFactory.createSMTPConnection();
                      smtpConn.send(smtpto, '', smtpfrom, smtpsubject, smtpbody);
                      //channelStatisticsController.clearStatistics( channelid, false, false, false, false, false, true);
                      $gc('queuealerted' + channelid, '0');
                      }
                      break;
                      }
                      }
                      }
                      }
                      }

                      Comment

                      Working...
                      X