Created
September 10, 2012 07:50
-
-
Save tommybotten/3689518 to your computer and use it in GitHub Desktop.
Example fact to determine the role of a server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fact: role | |
# | |
# Purpose: | |
# Return the installed and running applications on the system. | |
# | |
# FIXME: Add mongodb, amq, varnish, | |
# FIXME: As of now, interpreting/matching text and not return codes :( | |
Facter.add(:role) do | |
setcode do | |
introle = [] | |
# Check for MySQL | |
if mysqlstatus = Facter::Util::Resolution.exec('service mysqld status') \ | |
and mysqlstatus =~ /^mysqld \(pid .*\) is running\.\.\.$/ | |
introle << "mydb" | |
end | |
# And postgresql... | |
if pgsqlstatus = Facter::Util::Resolution.exec('service postgresql status') \ | |
and pgsqlstatus =~ /^postmaster \(pid .*\) is running\.\.\.$/ | |
# Perhpas this should be factored out...? | |
introle << "pgdb" | |
end | |
# And then apache ... | |
if apachestatus = Facter::Util::Resolution.exec('service httpd status') \ | |
and apachestatus =~ /^httpd \(pid .*\) is running\.\.\.$/ | |
# Perhpas this should be factored out...? | |
introle << "webfront" | |
end | |
# And Oracle | |
# And appserver / tomcat | |
# How to check this? Init scripts are not suited for this ... pgrep? ... | |
role = introle.join(", ") | |
end | |
end | |
Facter.add(:mydb) do | |
setcode do | |
dbs = Facter::Util::Resolution.exec("mysql -e 'show databases' -B | egrep -v '^Database$|^information_schema$|^mysql$'") | |
mydb = [] | |
mydb << dbs.split("\n") | |
mydb = mydb.join(", ") | |
mydb | |
end | |
end | |
Facter.add(:pgdb) do | |
setcode do | |
dbs = Facter::Util::Resolution.exec("sudo -u postgres psql -l --pset border=off 2>/dev/null | egrep '^[a-zA-Z]' | awk {'print $1'}") | |
pgdb = [] | |
pgdb << dbs.split("\n") | |
pgdb = pgdb.join(", ") | |
pgdb | |
end | |
end | |
# Note that this only works with named virtual hosts and not aliases. | |
Facter.add(:webfront_vhosts) do | |
setcode do | |
# testing purposes | |
vhosts = Facter::Util::Resolution.exec("apachectl -S 2> /dev/null | grep namevhost | awk {'print $4'} | sort | uniq") | |
webfront_vhosts = [] | |
webfront_vhosts << vhosts.split("\n") | |
webfront_vhosts = webfront_vhosts.join(", ") | |
webfront_vhosts | |
end | |
end | |
# Maybe this should check (using init scripts) wether the application actually runs? | |
# Check for /etc/init.d/tomcatsuperscript's symlinks. Iterate and check status for each. | |
# Check if the application is actually started and if supposed to start. Return some kind of list with this. | |
Facter.add(:tomcat_apps) do | |
setcode do | |
apps = Facter::Util::Resolution.exec("ls -1 /apps/tomcat/") | |
tomcat_apps = [] | |
tomcat_apps << apps.split("\n") | |
tomcat_apps = tomcat_apps.join(", ") | |
tomcat_apps | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment