logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
iptechnology  
#1 Posted : Wednesday, September 14, 2016 11:05:57 AM(UTC)
iptechnology


Rank: Newbie

Joined: 9/28/2015(UTC)
Posts: 3
Italy
Location: Brescia

Thanks: 1 times
Hi, this is our customization to the Offline Machine Notification extension..
The biggest changes are:
- More customizable parameter in the email subject and a full report of the session in the body
- overlapping of many offline session notifications now work great
- we use CustomProperty3 to check the machine status and to exclude it from the monitor (WARNING!! WE CLEAR CustomProperty3, SO USE THIS MOD ONLY IF YOU DON'T USE IT with other data!!)
- Email will be sent when the host that was previously down will return up

Please share your improvement to this script :-)

manifest.xml
Code:

<?xml version="1.0" encoding="utf-8"?>
<ExtensionManifest>
	<Version>1.0.3</Version>
	<Name>Offline Machine Notifications (Clone)</Name>
	<Author>ScreenConnect Labs</Author>
	<ShortDescription>This extension will automatically notify when a machine has been offline for longer than a timeframe specified.</ShortDescription>
	<Components>
		<DynamicSessionEventTriggerDefinition SourceFile="SessionEventTrigger.cs" MinProductVersionInclusive="5.5" />
	</Components>
	<Settings>
		<Setting Name="DelayedNotifier.EmailToAddress" IsServerOnly="true">
			<Description>The Email Address to where notifications are sent.  Will use default MailToAddress if no value is entered.</Description>
			<DefaultValue></DefaultValue>
		</Setting>
		<Setting Name="DelayedNotifier.EmailSubject" IsServerOnly="true">
			<Description>Format string for the notification email's subject.{0}=Session,{1}=Status,{2}=CustomProperty1,{3}=CustomProperty2</Description>
			<DefaultValue>[SC] Session {0} has {1} and remained in this state.</DefaultValue>
		</Setting>
		<Setting Name="DelayedNotifier.EmailBody" IsServerOnly="true">
			<Description>Format string for the notification email's body.</Description>
			<DefaultValue>Event occurred at {0} UTC.  This notification executed at {1} UTC after a {2} second delay.</DefaultValue>
		</Setting>
		<Setting Name="DelayedNotifier.DelayTimeSpanInSeconds" IsServerOnly="true">
			<Description>The amount of time, in seconds, to wait before checking connection status (MAX 120 seconds)</Description>
			<DefaultValue>10</DefaultValue>
		</Setting>
		<Setting Name="DelayedNotifier.MonitoredSessionFilter" IsServerOnly="true">
			<Description>A Session Group Filter denoting the Session's to be monitored.</Description>
			<DefaultValue>GuestOperatingSystemName LIKE '*' + 'server' + '*'</DefaultValue>
		</Setting>
	</Settings>
</ExtensionManifest>


SessionEventTrigger.cs
Code:

using System;
using System.Collections.Generic;
using ScreenConnect;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

public class SessionEventTriggerAccessor : IDynamicSessionEventTrigger
{
	Dictionary<string, System.Threading.Timer> Timer_List;
	
	System.Threading.Timer timer;
	System.Threading.Timer timer2;
	
	public Proc GetDeferredActionIfApplicable(SessionEventTriggerEvent sessionEventTriggerEvent)
	{
		try
		{
			if (Timer_List == null) { Timer_List = new 	Dictionary<string, System.Threading.Timer>(); }
			
			var Status = "";
			
			//  -- Gestione TIMER
			var extensionContext = ExtensionContext.Current;
			var version = (long)(DateTime.UtcNow - new DateTime(2013, 7, 18)).TotalSeconds;
			SessionFilterManager filterManager = new SessionFilterManager(() => version);
			filterManager.AddSession(sessionEventTriggerEvent.Session);
			filterManager.SetCurrentSessionEventTriggerEvent(sessionEventTriggerEvent);
			
			int delayIntervalInMilliseconds = Int32.Parse(extensionContext.GetSettingValue("DelayedNotifier.DelayTimeSpanInSeconds")) * 1000;
			if (delayIntervalInMilliseconds > 120000)
				delayIntervalInMilliseconds = 120000;
			else if (delayIntervalInMilliseconds <= 0)
				delayIntervalInMilliseconds = 5000;
				
				
			// -- Gestione OFFLINE	
			var sessionFilter = extensionContext.GetSettingValue("DelayedNotifier.MonitoredSessionFilter");
			var eventFilter = "Event.EventType = 'Disconnected' AND Session.SessionType = 'Access' AND Connection.ProcessType = 'Guest'";
			
			if (filterManager.DoesCurrentSessionEventMatchFilter(eventFilter) && filterManager.IsSessionInFilter(sessionFilter, sessionEventTriggerEvent.Session.SessionID))
			{
				Status = "#WENTDOWN#";
				
				if (Timer_List.ContainsKey(Status + sessionEventTriggerEvent.Session.SessionID.ToString()))
				{
					Timer_List[Status + sessionEventTriggerEvent.Session.SessionID.ToString()].Dispose();
					Timer_List.Remove(Status + sessionEventTriggerEvent.Session.SessionID.ToString());
				}
				
				Timer_List.Add(Status+sessionEventTriggerEvent.Session.SessionID.ToString(),
					               new System.Threading.Timer(x => TestSessionStatus(sessionEventTriggerEvent, extensionContext, Status), null, delayIntervalInMilliseconds, Timeout.Infinite) );
				
			}
			
			
		    //  -- Gestione ritorno ONLINE	
			var eventFilterRETURNUP = "Event.EventType = 'Connected' AND Session.CustomProperty3='#WENTDOWN#'";
			if (filterManager.DoesCurrentSessionEventMatchFilter(eventFilterRETURNUP))
			{
				Status = "#RETURNUP#";
				
				if (Timer_List.ContainsKey(Status + sessionEventTriggerEvent.Session.SessionID.ToString()))
				{
					Timer_List[Status + sessionEventTriggerEvent.Session.SessionID.ToString()].Dispose();
					Timer_List.Remove(Status + sessionEventTriggerEvent.Session.SessionID.ToString());
				}
				
				Timer_List.Add(Status+sessionEventTriggerEvent.Session.SessionID.ToString(),
					               new System.Threading.Timer(x => TestSessionStatus(sessionEventTriggerEvent, extensionContext, Status), null, delayIntervalInMilliseconds, Timeout.Infinite) );
					               
			}
		} 
		catch (Exception) 
		{
			//not important
		}
		return null;
	}
	

	
	public void TestSessionStatus(SessionEventTriggerEvent triggeredEvent, ExtensionContext context, string TestStatus)
	{
		try 
		{
			var sessionDetails = SessionManagerPool.Demux.GetSessionDetails(triggeredEvent.Session.SessionID);
			
			if (((sessionDetails.Session.ActiveConnections.Length == 0) && (TestStatus == "#WENTDOWN#")) || 
                ((sessionDetails.Session.ActiveConnections.Length == 1) && (TestStatus == "#RETURNUP#")) )  
			{	
				TagSession(triggeredEvent, context, TestStatus);
				SendNotification(triggeredEvent, context);
			}
			
			Timer_List[TestStatus+triggeredEvent.Session.SessionID.ToString()].Dispose();    
			Timer_List.Remove(TestStatus+triggeredEvent.Session.SessionID.ToString());
		}
		catch (Exception e)
		{
			//do nothing
		}
	}
	


	public static void SendNotification(SessionEventTriggerEvent triggeredEvent, ExtensionContext context)
	{
		try
		{
			using (var mailMessage = ServerToolkit.Instance.CreateMailMessage())
			{
				if (!string.IsNullOrEmpty(context.GetSettingValue("DelayedNotifier.EmailToAddress")))
					mailMessage.To.Add(context.GetSettingValue("DelayedNotifier.EmailToAddress"));
				else
					mailMessage.To.Add(AppSettingsCache.DefaultMailToAddress);
				
				mailMessage.Subject = String.Format(context.GetSettingValue("DelayedNotifier.EmailSubject"), triggeredEvent.Session.Name, triggeredEvent.SessionEvent.EventType.ToString(), triggeredEvent.Session.CustomPropertyValues[0],triggeredEvent.Session.CustomPropertyValues[1]);
				
				mailMessage.Body = String.Format(context.GetSettingValue("DelayedNotifier.EmailBody"), triggeredEvent.SessionEvent.Time, DateTime.UtcNow, context.GetSettingValue("DelayedNotifier.DelayTimeSpanInSeconds"));
	
				mailMessage.Body +=  Environment.NewLine +"______________________________________________________________________________________________________ "+ Environment.NewLine;
				mailMessage.Body +=  Environment.NewLine+"Tipo Sessione: ";
				mailMessage.Body +=  triggeredEvent.Session.SessionType;
				mailMessage.Body +=  Environment.NewLine+"Sistema Operativo: ";
				mailMessage.Body +=  triggeredEvent.Session.GuestInfo.OperatingSystemName.ToString();
				mailMessage.Body +=  Environment.NewLine+"Nome Sessione: ";
				mailMessage.Body +=  triggeredEvent.Session.Name;	
				mailMessage.Body +=  Environment.NewLine+"ID Sessione: ";
				mailMessage.Body +=  triggeredEvent.Session.SessionID;
				mailMessage.Body +=  Environment.NewLine+"Custom1: ";
				mailMessage.Body +=  triggeredEvent.Session.CustomPropertyValues[0];
				mailMessage.Body +=  Environment.NewLine+"Custom2: ";
				mailMessage.Body +=  triggeredEvent.Session.CustomPropertyValues[1];
				mailMessage.Body +=  Environment.NewLine+"Custom3: ";
				mailMessage.Body +=  triggeredEvent.Session.CustomPropertyValues[2];
				mailMessage.Body +=  Environment.NewLine+"Connessioni attive: ";
				mailMessage.Body +=  triggeredEvent.Session.ActiveConnections.Length;
				mailMessage.Body +=  Environment.NewLine;
				mailMessage.Body +=  Environment.NewLine+"Sessione Pubblica?: ";
				mailMessage.Body +=  triggeredEvent.Session.IsPublic;
				mailMessage.Body +=  Environment.NewLine;
				
				mailMessage.IsBodyHtml = false;
				new ScreenConnect.SmtpClient().Send(mailMessage);
			}
		} 
		catch (Exception)
		{
			//not important
		}
	}
	
	public static void TagSession(SessionEventTriggerEvent triggeredEvent, ExtensionContext context,string TAG)
	{
			//Imposta CustomProperty 3 (quindi CustomProperty[2])
			var  SessionCustomPropertyValues =  triggeredEvent.Session.CustomPropertyValues;
			SessionCustomPropertyValues[2]= TAG;  
			
			SessionManagerPool.Demux.UpdateSession("Controllore", triggeredEvent.Session.SessionID, triggeredEvent.Session.Name, triggeredEvent.Session.IsPublic, triggeredEvent.Session.Code, SessionCustomPropertyValues);
	}
	



}


Ciao
Alessandro

Edited by user Thursday, September 15, 2016 2:24:13 PM(UTC)  | Reason: quote -> code

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.