//---------------------------------------------------------------------------- // // Copyright (c) Intel Corporation, 2011 - 2012 All Rights Reserved. // //---------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; using Intel.Manageability.Exceptions; using Intel.Manageability; using Intel.Manageability.AgentPresence; namespace AgentPresenceLocalSample2 { static class AgentPresenceLocalFunctionality2 { public static void CreateOrUpdateAgent(IAMTInstance amt, string agentName) { // -------------------- // Create Agent // -------------------- var actions = new List { new AgentAction(AgentState.NotStarted, AgentState.Running, true, ActionSystemDefense.DeactivatePolicy, false), new AgentAction(AgentState.Stopped, AgentState.Expired | AgentState.Stopped, false, ActionSystemDefense.ActivatePolicy, false) }; Console.Write("Enter agent startup timeout in seconds (default is 120): "); if (!ushort.TryParse(Console.ReadLine(), out var startupTimer)) startupTimer = 120; Console.WriteLine(); Console.Write("Enter agent heartbeat timeout in seconds (default is 120): "); if (!ushort.TryParse(Console.ReadLine(), out var intervalTimer)) intervalTimer = 120; Console.WriteLine(); // Create policy and pass the action list as a parameter Agent agent = new Agent(agentName, actions, startupTimer, intervalTimer); // ------------------------------------------- // Add the agent to the Intel AMT instance // ------------------------------------------- try { amt.AgentPresence.Remote.CreateOrUpdateAgent(agent); Console.WriteLine("Create agent completed successfully."); } catch (ManageabilityException ex) { if ((ex.InnerException is AgentPresenceManageabilityException ex1) && (ex1.Failure == AgentPresenceFailure.DuplicateAgent)) { // Ignore the error, agent already exists Console.WriteLine("Agent already exists"); return; } throw ex; } } public static void Start(IAMTInstance amt, string applicationName) { // To start an agent, do the following: // 1. Create the Agent by invoking IAMTInstance.AgentPresence.Remote.CreateOrUpdateAgent(string) // with AgentName (see AgentPresenceRemoteSample sample). // 2. From AMT - local, register to watchdog and send heartbeats by invoking amt.AgentPresence.Local.StartPresence(). // When the StartPresence function sends heartbeats, it activates an endless loop that sends a heartbeat each agent.TimeoutInterval, // So, to avoid getting stuck on the StartPresence function, perform it in another thread. CreateOrUpdateAgent(amt, applicationName); ThreadPool.QueueUserWorkItem(agent => { try { Console.WriteLine("Start presence: " + applicationName.ToString()); amt.AgentPresence.Local.StartPresence(applicationName); } catch (ManageabilityException ex) { Console.WriteLine("Start presence failed with exception: " + ex.Message); } }); Console.WriteLine(applicationName + " is sending hearbeats, to stop press any key...."); Console.ReadKey(); } public static void Expire(IAMTInstance amt, string applicationName) { try { amt.AgentPresence.Local.ExpirePresence(applicationName); Console.WriteLine("Expire " + applicationName + " completed successfully."); } catch (ManageabilityException ex) { Console.WriteLine("Expire " + applicationName + " failed with exception: " + ex.Message); } } } }