Skip to content

Commit

Permalink
about-me: Determines date of birth of cloud VMs more accurately
Browse files Browse the repository at this point in the history
  • Loading branch information
markuslf committed Jul 16, 2024
1 parent 73dde87 commit eef9d44
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ Monitoring Plugins:
* graylog-version


### Changed ("enhancement")

Monitoring Plugins:

* about-me: Determines date of birth of cloud VMs more accurately


### Fixed ("bug")

Icinga Director:
Expand Down
35 changes: 11 additions & 24 deletions check-plugins/about-me/about-me
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import lib.dmidecode # pylint: disable=C0413
import lib.human # pylint: disable=C0413
import lib.net # pylint: disable=C0413
import lib.shell # pylint: disable=C0413
import lib.time # pylint: disable=C0413
import lib.txt # pylint: disable=C0413
import lib.version # pylint: disable=C0413
from lib.globals import STATE_OK, STATE_UNKNOWN # pylint: disable=C0413
Expand All @@ -40,7 +41,7 @@ except ImportError:


__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2024061901'
__version__ = '2024071601'

DESCRIPTION = 'Provides a quick overview of host dimensions and software.'

Expand Down Expand Up @@ -114,35 +115,21 @@ def parse_args():
def get_birthday():
"""Using various methods to determine install date.
"""
# Using stat
# the age of a machine is usually determined by the birthday of the root file system `/`.
# however, this does not work for cloud systems that are installed from pre-built images.
# in this case, the age of the cloud-init data must be used.
if os.path.exists('/var/lib/cloud/data/instance-id'):
# cloud-init based VM
birthday = os.stat('/var/lib/cloud/data/instance-id')
return 'born {}. '.format(lib.time.epoch2iso(birthday.st_ctime))
# no way to do this in python - getting the birthday of a folder
success, result = lib.shell.shell_exec(
'stat / | grep "Birth" | sed "s/Birth: //g" | cut -b 2-11',
shell=True,
)
birthday, _, _ = result
birthday = birthday.strip()
if birthday == '-':
birthday = ''
cmd = False
# nothing found so far, try more - but not all of those are very accurate
if os.path.isfile('/usr/bin/pacman'):
# Arch Linux, and Arch based distros using pacman
cmd = 'head -n 1 $PACMAN_LOG | cut -b 2-11'
elif os.path.isfile('/usr/bin/emerge'):
# Gentoo Linux and Gentoo based distros using portage
cmd = 'head -n 1 $PORTAGE_LOG | cut -b 31-43'
elif os.path.isfile('/usr/bin/rpm'):
# Fedora, RedHat, and RPM based distros
cmd = 'rpm -qi basesystem | grep "Install Date" | sed "s/Install Date: //g"'
if not cmd:
return ''
success, result = lib.shell.shell_exec(cmd)
if success:
birthday, _, _ = result
birthday = birthday.strip()
if birthday:
return 'born {}. '.format(birthday)
return ''
return 'born {}. '.format(birthday)


def get_boot_mode():
Expand Down

0 comments on commit eef9d44

Please sign in to comment.