trigger AWS Lambda function from S3 to windows with AWS SSM

Preparations;

– create your lambda, and connect trigger from s3 as shown below.
– grant permissions for your lambda. (use the key icon on the left)

 

– After installing SSM Agent to your Instance, grant AmazonSSM rights to your instance.

Action in python;

#09-03-2018 – Cem Dogan
import boto3
import json

def lambda_handler(event, context):
records = [x for x in event.get(‘Records’, []) if x.get(‘eventName’) == ‘ObjectCreated:Put’]
sorted_events = sorted(records, key=lambda e: e.get(‘eventTime’))
latest_event = sorted_events[-1] if sorted_events else {}
info = latest_event.get(‘s3’, {})
file_key = info.get(‘object’, {}).get(‘key’)
bucket_name = info.get(‘bucket’, {}).get(‘name’)
command = ‘C:\folder\your_PSscript.ps1 ‘ + file_key
print command
ssm = boto3.client(‘ssm’)
ssmresponse = ssm.send_command(InstanceIds=[‘i-instanceID’], DocumentName=’AWS-RunPowerShellScript’, Parameters= { ‘commands’: [command] } )
return ssmresponse

file_key will have the name of the file uploaded to s3 bucket. And bucket_name has the name of the bucket. You can pass these values as argument to your PS script. You can create an s3 put test example and test your code.

 

collecting IBM v7000 and Netapp storage information with PHP

I wrote this small code for our early morning checks to see the summary of IBM v700 and Netapp storage systems.

This simply collects Failed Disks, Aggregates, Volumes and battery status information and Alerts of each system for managerial reports on request. Or, can be situated at intranet portal.

Enjoy!

<?php
include_once(‘header.php’); //here you have session and menu information
//
//20-01-2016 – Cem DOGAN
//
// Report all PHP errors
#error_reporting(E_ALL);
//required php libraries for ssh connection. php net_ssh2
set_include_path(‘lib/’);
include(‘Net/SSH2.php’);
include(‘File/ANSI.php’);

$filer_username=”YOUR-READ-ONLY-USER”;
$filer_password=”YOUR-PASSWORD”;

function Visit_ssh($server, $username, $password, $command) {

#echo $server, $username, $password, $command;

$ssh = new Net_SSH2($server);
if (!$ssh->login($username, $password)) {
return (‘Login Failed’);
}
else {
return $ssh->exec($command);
}
}

/////////////////////////////////
//IBM v7000 Part /
/////////////////////////////////

$v7000_ip = array(
“AMS-SV7-01″=>”10.104.1.183”,
“DUS-SV7-01″=>”10.104.14.110”,
);

 

foreach($v7000_ip as $x => $x_value){
echo ‘<h6><u>’.$x.’ ‘.$curdate.’ : </u></h6>’;
echo ‘<pre>’;
echo “<u>Pool Usage;</u></br>”;
$filer_result=Visit_ssh($x_value, $filer_username, $filer_password, “lsmdiskgrp -delim :”);
// echo $filer_result;

$lines = explode(PHP_EOL, $filer_result); //put every line into array
$lines=array_filter($lines); //clean empty array objects

$line_counter=0;
$line_count=count($lines);
echo ‘<table>’;
foreach ($lines as $line) {
$item= explode(‘:’, $line);
echo ‘<tr>’;
if ($line_counter == 0) {
//display table header
echo “<th>$item[0]</th>”;//id
echo “<th>$item[1]</th>”;//name
echo “<th>$item[2]</th>”;//status
echo “<th>$item[5]</th>”;//capacity
echo “<th>$item[7]</th>”;//free capacity
echo “<th>$item[15]</th>”;//compression active

} else {
// table content
echo “<td>$item[0]</td>”;
echo “<td>$item[1]</td>”;
echo “<td>”;
if (preg_match(‘/online/’,$item[2])) {
echo “<b><font color=green>$item[2]</font></b>”;
}else {
echo “<font color=red>”.$item[2].”</font>”;
}
echo “</td>”;
echo “<td>$item[5]</td>”;
echo “<td>$item[7]</td>”;
echo “<td>$item[15]</td>”;
}
echo ‘</tr>’;
$line_counter++;
}
echo ‘</table>’;

echo “<u>Battery Status;</u> </br>”;
$filer_result=Visit_ssh($x_value, $filer_username, $filer_password, “lsenclosurebattery -delim :”);

$lines = explode(PHP_EOL, $filer_result); //put every line into array
$lines=array_filter($lines); //clean empty array objects

$line_counter=0;
$line_count=count($lines);
echo ‘<table>’;
foreach ($lines as $line) {
$item= explode(‘:’, $line);
echo ‘<tr>’;
if ($line_counter == 0) {
//display table header
echo “<th>$item[0]</th>”;//enclosure_id
echo “<th>$item[1]</th>”;//battery_id
echo “<th>$item[2]</th>”;//status
echo “<th>$item[3]</th>”;//charging_status
echo “<th>$item[4]</th>”;//recondition_needed
echo “<th>$item[5]</th>”;//percent_charged
echo “<th>$item[6]</th>”;//end_of_life_warning

} else {
// table content
echo “<td>$item[0]</td>”;
echo “<td>$item[1]</td>”;
echo “<td>”;
if (preg_match(‘/online/’,$item[2])) {
echo “<b><font color=green>$item[2]</font></b>”;
}else {
echo “<font color=red>”.$item[2].”</font>”;
}
echo “</td>”;
echo “<td>$item[3]</td>”;
echo “<td>$item[4]</td>”;
echo “<td>$item[5]</td>”;
echo “<td>$item[6]</td>”;
}
echo ‘</tr>’;
$line_counter++;
}
echo ‘</table>’;

echo “<u>Alert(s);</u> </br>”;
$filer_result=Visit_ssh($x_value, $filer_username, $filer_password, ‘lseventlog -filtervalue “status=alert” -fixed no -delim :’);

$lines = explode(PHP_EOL, $filer_result); //put every line into array
$lines=array_filter($lines); //clean empty array objects

$line_counter=0;
$line_count=count($lines);
echo ‘<table>’;
foreach ($lines as $line) {
$item= explode(‘:’, $line);
echo ‘<tr>’;
if ($line_counter == 0) {
//display table header
echo “<th>$item[0]</th>”;//sequence_number
echo “<th>$item[1]</th>”;//last_timestamp
echo “<th>$item[2]</th>”;//object_type
echo “<th>$item[3]</th>”;//object_id
echo “<th>$item[4]</th>”;//object_name
echo “<th>$item[6]</th>”;//status
echo “<th>$item[7]</th>”;//fixed
echo “<th>$item[10]</th>”;//description

} else {
// table content
echo “<td>$item[0]</td>”;
echo “<td>$item[1]</td>”;
echo “<td>$item[2]</td>”;
echo “<td>$item[3]</td>”;
echo “<td>$item[4]</td>”;
echo “<td>”;
if (preg_match(‘/alert/’,$item[6])) {
echo “<b><font color=red>$item[6]</font></b>”;
}
echo “</td>”;

echo “<td>”;
if (preg_match(‘/no/’,$item[7])) {
echo “<b><font color=red>$item[7]</font></b>”;
}
echo “</td>”;
echo “<td>$item[10]</td>”;
}
echo ‘</tr>’;
$line_counter++;
}
echo ‘</table>’;

 

echo ‘</pre>’;
}
/////////////////////////////////
//NetApp Part /
/////////////////////////////////

$storage_ip = array(
“ASD-FILER-A10A”=>”10.104.102.14”,
“ASD-FILER-A20A “=>”10.104.102.10”,
“ASD-FILER-A20B “=>”10.104.102.12”,
“DUS-FILER-A10A “=>”10.104.14.214”,
“DUS-FILER-A20A “=>”10.104.14.210”,
“DUS-FILER-A20B “=>”10.104.14.212”,
);

 

foreach($storage_ip as $x => $x_value){
echo ‘<h6><u>’.$x.’ ‘.$curdate.’ : </u></h6>’;
echo ‘<pre>’;
echo “<u>Failed disks;</u></br>”;
$filer_result=Visit_ssh($x_value, $filer_username, $filer_password, “vol status -f”);
if (preg_match(‘/empty/’,$filer_result)){
$filer_result = preg_replace( “/\r|\n/”, “”, $filer_result );
echo $filer_result.” -<b><font color=green> OK</font></b></br>”;
}
else {
echo “<font color=red>”.$filer_result.”</font>”;
}

 

echo “<u>Aggregates;</u> </br>”;
$filer_result=Visit_ssh($x_value, $filer_username, $filer_password, “df -Ah”);

$pattern=”snap”;
$lines = explode(PHP_EOL, $filer_result);
$exclude = array();

foreach ($lines as $line) {
if (strpos($line, ‘snap’) !== FALSE) {
continue;
}
preg_match(‘/(\d{2,3})\%/’,$line,$s_line); //match percentages
$s_line=array_filter($s_line); //clean empty array
if ( $s_line[1] >= 95 ) {
$line=str_replace($s_line[0],”<b><font color=red>”.$s_line[0].”</font></b>”,$line);
}
else {
$line=str_replace($s_line[0],”<b><font color=green>”.$s_line[0].”</font></b>”,$line);
}
$exclude[] = $line;
}
echo implode(“\n”, $exclude);
echo “<u>Volumes;</u> </br>”;
$filer_result=Visit_ssh($x_value, $filer_username, $filer_password, “df -h”);

$pattern=”snap”;
$lines = explode(PHP_EOL, $filer_result);
$exclude = array();

foreach ($lines as $line) {
if (strpos($line, ‘snap’) !== FALSE) {
continue;
}
preg_match(‘/(\d{2,3})\%/’,$line,$s_line); //match percentages
$s_line=array_filter($s_line); //clean empty array
if ( $s_line[1] >= 95 ) {
$line=str_replace($s_line[0],”<b><font color=red>”.$s_line[0].”</font></b>”,$line);
}
else {
$line=str_replace($s_line[0],”<b><font color=green>”.$s_line[0].”</font></b>”,$line);
}
$exclude[] = $line;
}
echo implode(“\n”, $exclude);

 

echo “<u>Environment;</u> </br>”;
$filer_result=Visit_ssh($x_value, $filer_username, $filer_password, “environment chassis”);

$lines = explode(PHP_EOL, $filer_result);
$exclude = array();

foreach ($lines as $line) {
preg_match(‘/ok/’,$line,$s_line); //match word
$s_line=array_filter($s_line); //clean empty array
if ( isset($s_line[0])) {
$line=str_replace($s_line[0],”<b><font color=green>”.strtoupper($s_line[0]).”</font></b>”,$line);
}
else{
$line=str_replace($s_line[0],”<b><font color=red>”.strtoupper($s_line[0]).”</font></b>”,$line);
}
$exclude[] = $line;
}
echo implode(“\n”, $exclude);

 

echo ‘</pre>’;
}

?>