Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Net.Mail;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Security;
namespace veeamBU
{
class Program
{
static void Main(string[] args)
{
try
{
string machineName = System.Environment.MachineName;
string bbWinDir = "c:\\hobbit";
string bbWinExt = "";
string dispName = "veeamBU";
string bbState = "green";
ArrayList jobs = new ArrayList();
jobs.Add("This is a test to determine if Veeam has either failures or warnings for");
jobs.Add("current jobs, and to test for long running jobs that may be hung.");
jobs.Add("\n");
jobs.Add("The jobs displayed have either returned a warning or failure, and the servers");
jobs.Add("that have problems are listed under the job with error/warning log entries.");
jobs.Add("\n");
jobs.Add("\n");
if (Directory.Exists("c:\\program files\\bbwin"))
{
bbWinDir = "c:\\program files\\bbwin\\tmp";
bbWinExt = "c:\\program files\\bbwin\\ext";
}
else if
(Directory.Exists("c:\\program files (x86)\\bbwin"))
{
bbWinDir = "c:\\program files (x86)\\bbwin\\tmp";
bbWinExt = "c:\\program files (x86)\\bbwin\\ext";
}
else if
(Directory.Exists("c:\\hobbit"))
{
bbWinDir = "c:\\hobbit\\tmp";
bbWinExt = "c:\\hobbit\\ext";
}
else
throw new Exception("Hobbit directory not found.");
try
{
Runspace rs = RunspaceFactory.CreateRunspace();
rs.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddScript("Add-PSSnapin VeeamPSSnapin");
ps.AddScript("Get-VBRJob | where {($_.GetLastResult() -eq \"Failed\") -OR ($_.GetLastResult() -eq \"Warning\")} | select name");
foreach (PSObject result in ps.Invoke())
{
jobs.Add(result.Members["Name"].Value);
jobs.Add("-------------");
jobs.Add("\n");
Runspace rs2 = RunspaceFactory.CreateRunspace();
rs2.Open();
PowerShell ps2 = PowerShell.Create();
ps2.Runspace = rs2;
ps2.AddScript("Add-PSSnapin VeeamPSSnapin");
ps2.AddScript("$Job = Get-VBRJob -name " + result.Members["Name"].Value);
ps2.AddScript("$LastSession = $Job.FindLastSession()");
ps2.AddScript("$LastSession | Get-VBRTaskSession | ?{($_.status -eq \"Warning\") -OR ($_.status -eq \"Failed\")} | select name");
foreach (PSObject result2 in ps2.Invoke())
{
jobs.Add(result2.Members["Name"].Value);
ps2.AddScript("$TaskSession = $LastSession | Get-VBRTaskSession | where {$_.name -eq \"" + result2.Members["Name"].Value + "\"}");
ps2.AddScript("$TaskSession.Logger.GetLog().updatedrecords | sort starttime | select title");
foreach (PSObject result3 in ps2.Invoke())
{
if (result3.Members["title"].Value.ToString().Contains("Warning") ||
result3.Members["title"].Value.ToString().Contains("Failed") ||
result3.Members["title"].Value.ToString().Contains("Error"))
{
string tempAdd = result3.Members["title"].Value.ToString();
//Console.WriteLine(tempAdd);
if (tempAdd.Count() >= 100)
{
do
{
jobs.Add(tempAdd.Substring(0, 99) + "-");
tempAdd = tempAdd.Substring(99);
} while (tempAdd.Count() >= 100);
jobs.Add(tempAdd);
}
else
{
jobs.Add(tempAdd);
}
}
}
jobs.Add("\n");
jobs.Add("\n");
}
}
rs.Close();
}
catch(Exception e)
{
jobs.Add(e.ToString());
}
try
{
Runspace rs = RunspaceFactory.CreateRunspace();
rs.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddScript("Add-PSSnapin VeeamPSSnapin");
ps.AddScript("Get-VBRJob | select name, {$_.Findlastsession().creationTime}, {$_.Findlastsession().EndTime}, {$_.Findlastsession().IsFullMode}");
jobs.Add("Job Name\tStart Time\t\tEnd Time\t\tIs Full mode");
jobs.Add("--------\t----------\t\t--------\t\t------------");
foreach (PSObject result in ps.Invoke())
{
string addLine = result.ToString();
string name = "";
string sTime = "";
string eTime = "";
string isFull = "";
addLine = addLine.Remove(0, addLine.IndexOf("Name=") + 5);
name = addLine.Substring(0, addLine.IndexOf(";"));
addLine = addLine.Remove(0, addLine.IndexOf("creationTime=") + 13);
sTime = addLine.Substring(0, addLine.IndexOf(";"));
addLine = addLine.Remove(0, addLine.IndexOf("EndTime=") + 8);
eTime = addLine.Substring(0, addLine.IndexOf(";"));
addLine = addLine.Remove(0, addLine.IndexOf("IsFullMode=") + 11);
isFull = addLine.Substring(0, addLine.IndexOf("}"));
jobs.Add(name + "\t" + sTime + "\t" + eTime + "\t" + isFull);
if (eTime.Contains("1/1/1900") && Convert.ToDateTime(sTime) < DateTime.Now.AddHours(-6) && isFull == "False")
{
jobs.Add("&red This incremental job is running too long!!!");
}
else if (eTime.Contains("1/1/1900") && Convert.ToDateTime(sTime) < DateTime.Now.AddHours(-24) && isFull == "True")
{
jobs.Add("&red This full backup job is running too long!!!");
}
jobs.Add("\n");
}
rs.Close();
}
catch (Exception e)
{
jobs.Add(e.ToString());
}
string fileName = bbWinDir + "\\" + dispName;
if (File.Exists(fileName))
File.Delete(fileName);
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.WriteLine(bbState);
foreach (string allJobs in jobs)
{
Console.WriteLine(allJobs);
sw.WriteLine(allJobs);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}