Skip to content

Instantly share code, notes, and snippets.

View Sam-Martin's full-sized avatar

Sam Martin Sam-Martin

View GitHub Profile
@Sam-Martin
Sam-Martin / Lambda Delete Test Kitchen Instances after 24hrs.py
Last active November 27, 2017 16:15
Lambda Delete Test Kitchen Instances after 24hrs
import boto3
from datetime import datetime
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
@Sam-Martin
Sam-Martin / Remove-OldbeatsIndices.ps1
Created November 23, 2017 10:14
Remove-OldbeatsIndices.ps1
$ElasticSearchURL = 'https://<elasticsearchurl>:<port>'
$indices = (iwr "$ElasticSearchURL/_cat/indices?format=json&pretty").content | ConvertFrom-Json
$indices.Index | ?{$_ -match '\d{4}\.\d\d\.\d\d$'} | sort
$IndicesWithDate = $indices | ?{$_.index -like '*beat-*'} | select index,@{L="Date";E={get-date ($_.index -split '-')[1]}}
$indicesToDelete = $IndicesWithDate | ?{$_.date -lt (Get-Date).AddDays(-7)}
foreach($index in $indicesToDelete){
$index.index
iwr "$ElasticSearchURL/$($index.index)" -Method delete
@Sam-Martin
Sam-Martin / Sams_datadog_sensors.py
Last active November 23, 2017 00:41
Sam's Server's Datadog Sensors
from subprocess import check_output
from datadog import statsd
from datadog.api.constants import CheckStatus
import requests
import re
zpool_status = check_output(["/sbin/zpool", "status"])
pattern = re.compile('state: ONLINE')
@Sam-Martin
Sam-Martin / Get-CPUUtilisationAllInstancesMultipleAccounts.ps1
Created November 14, 2017 14:05
Get-CPUUtilisationAllInstancesMultipleAccounts.ps1
$ProfileNames = @(
"Profile1",
"Profile2"
)
foreach($Profile in $ProfileNames){
Set-AWSCredential -ProfileName $Profile
$StartTime = (Get-Date).AddDays(-30)
$EndTime = (Get-Date)
@Sam-Martin
Sam-Martin / Copy-RecentSnapshotsToNewRegion.ps1
Last active September 14, 2017 13:49
Copy All snapshot for the last 24hrs to another region
$Env:AWSAccountID = "ACCOUNTID"
$Env:SourceRegion = "eu-central-1"
$ENV:DestinationRegion = "eu-west-1"
$env:AgeOfSnapshotsDays=1
$SourceSnapshots = Get-EC2Snapshot -region $Env:SourceRegion -OwnerId $Env:AWSAccountID
$SourceFiltered = $SourceSnapshots | ?{$_.starttime -gt (get-date).AddDays(-1*$env:AgeOfSnapshotsDays)}
Write-Verbose "Found $($SourceFiltered.count) snapshots to copy from $Env:SourceRegion to $Env:DestinationRegion"
@Sam-Martin
Sam-Martin / gist:9f6bba63785fd3071882c6c313210520
Created August 8, 2017 12:35
PowerShell rename and join computer to computer
Rename-Computer -NewName $newComputerName
Add-Computer -Credential $Credentials -DomainName 'SDLPRODUCTS.com' -Force -Options JoinWithNewName,AccountCreate
@Sam-Martin
Sam-Martin / Colour-output.ps1
Created July 18, 2017 07:48
PowerShell Coloured output (stolen from someone somewhere)
function Format-AnsiColor {
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
[AllowEmptyString()]
[String]
@Sam-Martin
Sam-Martin / add-winrm.ps1
Created July 5, 2017 14:11
Add WinRM to all SecurityGroups that have RDP
$Ips = @("192.168.1.1")
foreach($Region in $(Get-AWSRegion).Region){
$SecurityGroupsContainingRDP = Get-EC2SecurityGroup -region $Region| ?{$_.ippermission.FromPort -eq 3389}
Foreach($SecurityGroup in $SecurityGroupsContainingRDP){
Write-Verbose "Adding WinRM to $($SecurityGroup.GroupID)"
try{
Grant-EC2SecurityGroupIngress -GroupId $SecurityGroup.GroupId -region $Region -IpPermission @{IpProtocol="TCP";FromPort=5985;ToPort=5986;IPRanges=$IPs}
}catch{
if($_.exception.message -like "*already exists*"){
Write-Verbose "`tRule already exists"
@Sam-Martin
Sam-Martin / Clear-JenkinsBuildHistory.ps1
Created July 4, 2017 09:48
Clean up Jenkins Build history
$BuildDirs = gci "W:\Jenkins\jobs\" -recurse -filter builds | ?{$_.psiscontainer}
$BuildDirs | %{
$Builds = $_ | gci | select *,@{L="NameInt";e={[int]$_.name}} | ?{$_.nameint}| sort nameint -Descending
if($builds.Length -gt 30){
$Builds[30..$($builds.length-1)].fullname | Remove-Item -Force -Recurse -verbose
}else{
$Builds.Length
}
}
@Sam-Martin
Sam-Martin / datadog_zpool_status_check.py
Last active June 11, 2017 09:11
DataDog zpool status check
from subprocess import check_output
from datadog import statsd
from datadog.api.constants import CheckStatus
import re
zpool_status = check_output(["/sbin/zpool", "status"])
pattern = re.compile('state: ONLINE')
if pattern.search(zpool_status) :