GithubHelp home page GithubHelp logo

samchiang / esxidown Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thisistenou/esxidown

0.0 0.0 0.0 39 KB

A shell script to gracefully shutdown all VMs on an ESXi host, followed by the host itself. Works great for maintenance operations or automated shutdowns in case of emergencies / power loss.

License: MIT License

Shell 100.00%

esxidown's Introduction

ESXi Auto Shutdown Script

Tested on ESXi 7.0.1 (Enterprise Plus).

Based on the work of Jon Saltzman, Andriy Babak and Sophware.

How does this script differ from the numerous, other forks?

  • VM shutdown commands are run in parallel, greatly reducing the total time needed until the host can shut down.
  • Command outputs are written to a log file
  • Script- and log-locations can be hardcoded or just left blank - they'll default to where async.sh resides.

What are the different files for?

The only interesting files are the two scripts, "esxidown.sh" and "async.sh".

esxidown.sh is the script doing all the magic. When called, it will put your ESXi-Host into maintenance mode, send a shutdown command to all running VMs, wait for them to shutdown and then proceed to shutdown the phyiscal host, leaving maintenance mode again in the meantime. The "leaving maintenance mode"-part can technically fail, but at least in my experience, it's always been working fine and allows the VMs to autostart after the host has been brought up again.

async.sh is a small wrapper, which tells the esxidown.sh-script everything it needs, like custom variables. Additionally, it executes esxidown.sh asynchronously (hence the name) in the background instead of the current shell. This means that you can leave the (ssh-)session, without having to wait for the script to finish first. All output is being sent to the logfile, not the console.

How do I use it?

  1. Copy both "esxidown.sh" and "async.sh" onto the datastore of your ESXi hosts. You can either do this through vCenter's WebUI or via SFTP/SSH.
  2. Open up the async.sh-script in VI and edit the variables to match your environment. If no script-/log-path is set, it'll default to the directory in which async.sh resides.
  3. Make both scripts executable: chmod +x esxidown.sh async.sh
  4. Congrats! You can now safely shutdown your ESXi host just by calling the async.sh-script.

Important notes:

  • Make sure that both paths are the absolute paths, starting at root level. Also make sure that all subsequent directories for the log location exist, otherwise it'll fail to be created there and default to where async.sh resides.
  • STORE THE FILES ON A DATASTORE. Like, honestly. They gotta be on a datastore. If you just throw them into the root-directory, they will NOT survive a reboot. This applies to both the scripts and the logfile. A log doesn't help you if it's gone seconds after ;)

I want to automate this to run when a power loss occurs. Is that possible?

Certainly! In my setup, my monitoring software is calling these scripts via ssh on all hosts. Here are some command snippets you can base your automation on:

Interactive oneliner:

ssh [email protected] '/bin/sh /vmfs/volumes/yourdatastore/async.sh'

Non-interactive oneliner using authentication keys:

ssh -i ./.ssh/my-key.pem [email protected] '/bin/sh /vmfs/volumes/yourdatastore/async.sh'

Non-Interactive expect-script using passwords: Has to be called like this: /usr/bin/expect script.exp esxi.lab.local pa$sw0rd

#!/usr/bin/expect
set host [lindex $argv 0]
set password [lindex $argv 1]

spawn ssh root@$host
expect {
  "Are you sure you want to continue connecting" {send "yes\r";exp_continue}
  "Password:" {send "$password\r";exp_continue}
}
expect ":~]"
send "/bin/sh /vmfs/volumes/yourdatastore/async.sh"
send "\r"
expect ":~]"
send "exit\r"
expect eof

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.