Wednesday, March 15, 2017

Power shell to stop running Azure VM based on tags

$RGs =@()
$processed_vm =@()
$RGs = $(Get-AzureRMResourceGroup |Select-Object -Property  ResourceGroupName -Verbose:$false )
foreach ($RG in $RGs)
{
$ResourceGroupName = $RG.ResourceGroupName

 $vm_list = Get-AzureRmVM -ResourceGroupName $ResourceGroupName |  ? (  {($_.Tags.Keys -notcontains "Do Not Disturb") -or ($_.Tags.Keys -eq "Do Not Disturb" -and $_.Tags.Values -eq "No")   })
 foreach($vm in $vm_list)
 {
 $vm_name=$vm.Name

 Get-AzureRmVM -name $vm_name -Status -ResourceGroupName $ResourceGroupName | Select-Object -Property Name, Statuses|
  ForEach-Object  {
        $_.Statuses |
        Where-Object {$_.Code -like 'PowerState/*'} |
        ForEach-Object {
          New-Object -TypeName psobject -Property @{
           Status = $_.DisplayStatus
                     
          }
          if ( $_.DisplayStatus -eq "VM running" -And  $(Get-AzureRmResource -ResourceGroupName "$ResourceGroupName" -Name $VMName).Tags.'Do Not Disturb' -eq "No" ){

           #Stop-AzurermVM -ResourceGroupName "$ResourceGroupName" -Name $VMName -Force -ErrorAction SilentlyContinue
           $processed_vm += $vm_name

             
            }
         
        }
       
       
      }



 }
 

  }
 
write-output "  List of VMs Stopped " |Out-File vmlist.txt
write-output "-----------------------"  | Add-Content vmlist.txt
$processed_vm | Add-Content vmlist.txt

Friday, February 10, 2017

Terraform



provider "azurerm" {
  subscription_id = "XXXXXXXXXXXXXXXXXXXXX"
  client_id       = "XXXXXXXXXXXXXXXXXXXxx"
  client_secret   = "XXXXXXXXXXXXXX"
  tenant_id       = "XXXXXXXXXXXXXXXXXXXXX"
}

# create a resource group
resource "azurerm_resource_group" "dhanesh-rs" {
  name = "terraformtest"
  location = "Southeast Asia"
}

resource "azurerm_virtual_network" "dhanesh-net"{

  address_space = ["10.0.0.0/16"]
  location = "Southeast Asia"
  name = "dhanesh-net"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
}

resource "azurerm_subnet" "dhanesh-sub" {
  address_prefix = "10.0.2.0/24"
  name = "dhanesh-sub"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
  virtual_network_name = "${azurerm_virtual_network.dhanesh-net.name}"
}

resource "azurerm_network_interface" "dhanesh-if" {

  location = "Southeast Asia"
  name = "dhanesh-if"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"

  "ip_configuration" {

    name = "dhanesh-ifip"
    private_ip_address_allocation = "dynamic"
    subnet_id = "${azurerm_subnet.dhanesh-sub.id}"
  }
}

resource "azurerm_availability_set" "dhanesh-as" {

  name = "dhanesh-az"
  location = "Southeast Asia"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
  tags{
    environment="production"
  }

}

resource "azurerm_storage_account" dhanesh-ds {

  name = "dhaneshds"
  account_type = "Standard_LRS"
  location = "Southeast Asia"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
  tags {
    environment ="production"
  }
}

resource "azurerm_storage_container" "dhansh-sc" {
  name = "dhanesh-sc"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
  storage_account_name = "${azurerm_storage_account.dhanesh-ds.name}"
  container_access_type = "private"
}

resource "azurerm_virtual_machine" "dhanesh-vm" {
  name = "dhaneshvm"
  location = "Southeast Asia"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
  network_interface_ids = ["${azurerm_network_interface.dhanesh-if.id}"]
  vm_size = "Standard_A0"


  storage_image_reference {
    publisher = "Canonical"
    offer = "UbuntuServer"
    sku = "14.04.2-LTS"
    version = "latest"
  }

  storage_os_disk {
    name ="myosdisk"
    vhd_uri = "${azurerm_storage_account.dhanesh-ds.primary_blob_endpoint}${azurerm_storage_container.dhansh-sc.name}/myos.vhd"
    caching = "ReadWrite"
    create_option = "FromImage"
  }

  os_profile {

    admin_password = "Dhanesh@1234567"
    admin_username = "dhanesh"
    computer_name = "dhanesh-vm"
  }

  os_profile_linux_config {

    disable_password_authentication = false
  }

  tags {
   environment = "production"
  }

}

Thursday, February 9, 2017

Important Linux logs for auditing


/var/log/message – Whole system logs
/var/log/auth.log – Authentication logs.
/var/log/kern.log – Kernel logs.
/var/log/cron.log – Crond logs (cron job).
/var/log/maillog – Mail server logs.
/var/log/boot.log – System boot log.
/var/log/mysqld.log – MySQL database server log file.
/var/log/secure – Authentication log.
/var/log/utmp or /var/log/wtmp : Login records file.
/var/log/yum.log: Yum log files.

Removing sudo account


/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync

Tuesday, February 7, 2017

Creating Load in Linux Servers

Stress for Auto scaling testing [LINUX] 

For performance testing or auto scale testing we usually search for a method to generate CPU / Memory  load generator. Here is one of the tool which I have used for generating CPU load to test  auto scaling in Microsoft Azure is "stress" 

By default I didn't find the stress package in Centos so I had to enable epel repository .


yum install epel-release 
yum install stress

Monday, December 26, 2016

Creating multiple VM using Vagrant

This is a sample configuration for vagrant to create custom stack of servers.

Steps1. create file  "Vagrantfile"


#--DDL
require 'yaml'
records = YAML.load_file('config.yaml')
$x = records['server_name'].size
puts "You have asked to install #{$x} of systems "
puts "#{records['server_name']}"
cluster  = {}
(0..($x-1)).each do |i|
cluster["hostname" => "#{records['server_name'][i]['hostname']}" , "ip" => "#{records['server_name'][i]['ip']}"]="hostname-#{i}"
end
Vagrant.configure(2) do |config|

  cluster.each_with_index do |(info, hostname), index|

    config.vm.define hostname do |cfg|
      cfg.vm.provider :virtualbox do |vb, override|
        config.vm.box = "centos/7"
override.vm.synced_folder '.', '/vagrant' , type: "virtualbox"
override.vm.network :private_network, ip: "#{info['ip']}"
        override.vm.hostname = "#{info['hostname']}"
        vb.name = "#{info['hostname']}"
override.vm.provision "ansible_local" do |ansible|
ansible.playbook = "playbook.yaml"
end
      end # end provider
    end # end config

  end # end cluster
end


step 2. Create file config.yaml ( --- also needs to include )


---
 server_name:
         - hostname: "webserver"
           ip: "10.0.15.2"
         - hostname: "database"
           ip: "10.0.15.3"
         - hostname: "appserver"
           ip: "10.0.15.4"




step3. place one sample "playbook.yaml" file


---
- hosts: all
  become: yes
  tasks:
    - name: ensure ntpd is at the latest version
      yum: pkg=ntp state=latest
      notify:
      - restart ntpd
  handlers:
    - name: restart ntpd
      service: name=ntpd state=restarted



step4. run "vagrant up"



Note:  I got some issues while  starting vagrant related to sharing the directory to guest vm. At that time I used following command to resolve it.    "vagrant plugin install vagrant-vbguest "




Please test it and send me if there is any issues. 

Thursday, October 27, 2016

ANSIBLE

to get host inventory 
- name: Install this only for local dev machine
  pip: name=pyramid
  when: inventory_hostname == "local"
     instead of inventory_hostname , can use group_names to get individual group
copy command fails for large files use synchronize
tasks:
    - name: Transfer file from ServerA to ServerB
      synchronize:
        src: /path/on/server_a
        dest: /path/on/server_b


Which operating system you like most?