108 lines
4.7 KiB
C#

//----------------------------------------------------------------------------
//
// Copyright (c) Intel Corporation, 2011 - 2012 All Rights Reserved.
//
//----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Intel.Manageability.Exceptions;
using Intel.Manageability;
using Intel.Manageability.AgentPresence;
namespace AgentPresenceLocalSample1
{
static class AgentPresenceLocalFunctionality1
{
public static void CreateOrUpdateAgent(IAMTInstance amt, string agentName)
{
// --------------------
// Create Agent
// --------------------
var actions = new List<AgentAction>
{
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;
}
}
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 Shutdown(IAMTInstance amt, string applicationName)
{
try
{
amt.AgentPresence.Local.ShutdownPresence(applicationName);
Console.WriteLine("Shutdown " + applicationName + " completed successfully.");
}
catch (ManageabilityException ex)
{
Console.WriteLine("Shutdown " + applicationName + " failed with exception: " + ex.Message);
}
}
}
}