運維開發入門記錄

 

後來 找了小半下午,python+salt 就能夠搞定。node

很少廢話 直接開搞:python

思路以下 ,直接畫圖:mysql

架構草圖linux

 

下來這個是基本拓撲:ios

 

 




這是一個 用python實現 調用salt的實例,具體思路是這樣的
salt 咱們介紹一下吧,簡單點的,好了咱們來看這玩意幹嗎的
SALTSTACK

Salt, a new approach to infrastructure management, is easy enough to get running in minutes, scalable enough to manage tens of thousands of servers, and fast enough to communicate with those servers in seconds.

Salt delivers a dynamic communication bus for infrastructures that can be used for orchestration, remote execution, configuration management and much more.
大概意思是
salt一種新的基礎設施管理的方法,是很容易獲得運行在幾分鐘內,可擴展到管理成千上萬的服務器,並快速足夠的溝通與這些服務器在幾秒鐘。
salt提供了一個動態的通訊總線的基礎設施,可用於業務流程,遠程執行,配置管理和更多......。

那咱們先來安裝salt  
安裝很簡單 ,樓主是virtualBOX創建兩個虛擬機裝有linux centos6.5 ,裝master 和mini  就是主控制機器 和被控制機器,對沒錯是被控制哦,SM 嘻嘻嘻嘻~咱們想幹嗎幹嗎的那種 滴蠟 小皮鞭什麼的。
虛擬機兩臺,saltmaster 10.219.25.241 
                     minion 10.219.25.242nginx

 

下面是安裝腳本
master:redis

#!/usr/bin/bash
cd /usr/local/src/;
wget http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm;
rpm -ivh epel-release-6-8.noarch.rpm;
wget http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm;
rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm;
yum install python-jinja2;
yum -y install salt-master enablerepr=epel-testing;

minion:sql

#!/usr/bin/bash
cd /usr/local/src/;
wget http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm;
rpm -ivh epel-release-6-8.noarch.rpm;
wget http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm;
rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm;
yum install python-jinja2;
yum -y install salt-minion enablerepr=epel-testing;

裝完改配置,*注意空格:shell

Masterjson

vim /etc/salt/master
# The address of the interface to bind to:
  interface: 10.219.25.241

mini

vim /etc/salt/minion
# resolved, then the minion will fail to start.
master: 10.219.25.241

關閉兩臺機器防火牆

/etc/init.d/iptables stop

啓動兩臺機器的salt服務

master:

/etc/init.d/salt-master start

mini:

/etc/init.d/salt-minion start

先發證書:

[root@salt-master ~]# salt-key -L
Accepted Keys:
salt-master   //這裏是娶回家的
slave01  
slave02
Denied Keys:
Unaccepted Keys:  //這裏是小情人  要接進來的 
Rejected Keys:

 

接受證書:

[root@salt-master ~]# salt-key -A 
點Y  回車

測試下能夠調教了不,先來麼麼噠:

[root@salt-master ~]# salt \* test.ping
slave02:
    True
slave01:
    True

OK沒問題

接下來是重頭戲
salt 是python寫的 提供了api調用
咱們裝上salt api
比較簡單

*這裏只須要在master端安裝:

yum -y install salt-api

進入python 注意 這裏python是2.6.6的:[centos6.5 redhat6.5 默認安裝2.6.6python  ubuntu 爲python2.7]

作個簡單的測試,執行cmd.run沒有問題,返回結果是dict字典

>>> import salt.client
>>> local = salt.client.LocalClient()
>>> local.cmd('*','cmd.run',['whoami'])
{'slave01': 'root'}

咱們獲取靜態信息試試:

>>> local.cmd('*','grains.items',)
{'slave01': {'biosversion': 'VirtualBox', 'kernel': 'Linux', 'domain': '', 'zmqversion': '3.2.5', 'kernelrelease': '2.6.32-431.el6.x86_64', 'selinux': {'enforced': 'Enforcing', 'enabled': True}, 'serialnumber': '0', 'ip_interfaces': {'lo': ['127.0.0.1', '::1'], 'eth1': ['10.219.25.242', 'fe80::a00:27ff:fe8b:263e']}, 'shell': '/bin/bash', 'mem_total': 996, 'saltversioninfo': [2015, 5, 8, 0], 'host': 'slave01', 'SSDs': [], 'mdadm': [], 'id': 'slave01', 'osrelease': '6.5', 'ps': 'ps -efH', 'server_id': 1039472111, 'ip6_interfaces': {'lo': ['::1'], 'eth1': ['fe80::a00:27ff:fe8b:263e']}, 'num_cpus': 1, 'hwaddr_interfaces': {'lo': '00:00:00:00:00:00', 'eth1': '08:00:27:8b:26:3e'}, 'init': 'upstart', 'ip4_interfaces': {'lo': ['127.0.0.1'], 'eth1': ['10.219.25.242']}, 'osfullname': 'CentOS', 'master': '10.219.25.241', 'ipv4': ['10.219.25.242', '127.0.0.1'], 'ipv6': ['::1', 'fe80::a00:27ff:fe8b:263e'], 'cpu_flags': ['fpu', 'vme', 'de', 'pse', 'tsc', 'msr', 'pae', 'mce', 'cx8', 'apic', 'sep', 'mtrr', 'pge', 'mca', 'cmov', 'pat', 'pse36', 'clflush', 'mmx', 'fxsr', 'sse', 'sse2', 'syscall', 'nx', 'rdtscp', 'lm', 'constant_tsc', 'up', 'rep_good', 'xtopology', 'nonstop_tsc', 'unfair_spinlock', 'pni', 'pclmulqdq', 'monitor', 'ssse3', 'cx16', 'sse4_1', 'sse4_2', 'movbe', 'popcnt', 'aes', 'xsave', 'avx', 'rdrand', 'hypervisor', 'lahf_lm', 'abm'], 'localhost': 'slave01', 'lsb_distrib_id': 'CentOS', 'fqdn_ip4': [], 'fqdn_ip6': [], 'nodename': 'slave01', 'saltversion': '2015.5.8', 'lsb_distrib_release': '6.5', 'pythonpath': ['/usr/bin', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info'], 'saltpath': '/usr/lib/python2.6/site-packages/salt', 'pythonversion': [2, 6, 6, 'final', 0], 'osmajorrelease': '6', 'os_family': 'RedHat', 'oscodename': 'Final', 'osfinger': 'CentOS-6', 'biosreleasedate': '12/01/2006', 'manufacturer': 'innotek GmbH', 'num_gpus': 1, 'virtual': 'VirtualBox', 'cpu_model': 'Intel(R) Core(TM) i5-4200H CPU @ 2.80GHz', 'fqdn': 'slave01', 'pythonexecutable': '/usr/bin/python2.6', 'productname': 'VirtualBox', 'osarch': 'x86_64', 'cpuarch': 'x86_64', 'lsb_distrib_codename': 'Final', 'osrelease_info': [6, 5], 'locale_info': {'detectedencoding': 'UTF-8', 'defaultlanguage': 'en_US', 'defaultencoding': 'UTF8'}, 'gpus': [{'model': 'VirtualBox Graphics Adapter', 'vendor': 'unknown'}], 'path': '/sbin:/usr/sbin:/bin:/usr/bin', 'machine_id': 'c208c39c323edb1ac57c14a50000000b', 'os': 'CentOS'}}

 

返回字典python操做方法

radiansdict.values()
以列表返回字典中的全部值
radiansdict.keys()
以列表返回一個字典全部的鍵

這是百度到別人處理這類字符串 整合爲表格的代碼對我啓發很大:

#coding=utf-8
import salt.client as sc
import json
###salt調用
local = sc.LocalClient()
###目標主機指定
tgt = "*"
###獲取grains,disk信息
grains = local.cmd(tgt,"grains.items")
diskusage = local.cmd(tgt,"disk.usage")
###主要應用列表即文件開頭
app_name = ["tomcat","zookeeper","redis","mysql","nginx"]
cols = "主機名,IP地址,內存(GB),CPU核數,操做系統,數據盤/data(GB),所屬項目,主要應用"
###打開一個.csv文件,以便寫入
ret_file = open("ret.csv","w")
###首先寫入開頭,有點字段名的意思
ret_file.write(cols + "\n")
try:
for i in grains.keys():
###打印信息可註釋掉
print grains[i]["nodename"]
print "ipv4" + ":" ,grains[i]["ipv4"]
print "mem_total" + ":" , grains[i]["mem_total"] / 1024 + 1
print "num_cpus" + ":" , grains[i]["num_cpus"]
print "osfullname" + ":" , grains[i]["osfullname"]
print "release" + ":" , grains[i]["lsb_distrib_release"]
###可能一些主機沒有/data數據盤1048576是1024x1024
if "/data" not in diskusage[i]:
print "diskusage" + ":" + "have no /data disk"
else:
data_vol = int(diskusage[i]["/data"]["1K-blocks"])
print "diskusage" + ":" , data_vol / 1048576
###去掉127.0.0.1這個地址
ipv4 = str(grains[i]["ipv4"]).replace(", '127.0.0.1'","")
###由於一些歷史遺留問題,這裏取得不是主機名,而是salt-minion的id名,用以判斷主要應用
hostname = grains[i]["id"]
ipv4 = str(grains[i]["ipv4"]).replace(", '127.0.0.1'","")
ipv4 = ipv4.replace(",","and")
mem = grains[i]["mem_total"] / 1024 + 1
num_cpu = grains[i]["num_cpus"]
OS = grains[i]["osfullname"] + grains[i]["lsb_distrib_release"]
if "/data" not in diskusage[i]:
disk_data = "None"
else:
disk_data = data_vol / 1048576
###項目名爲空
project = ""
###經過minion ID名來判斷主要運行服務,好比xx-mysql-1,則運行mysql
for j in app_name:
if j in hostname.lower():
app = j
break
else:
app = "undefined"
c = ","
###鏈接並寫入
line = hostname + c + ipv4 + c + str(mem) + c + str(num_cpu) + c + str(OS) + c + str(disk_data) + c + project + c + app
ret_file.write(line + "\n")
except Exception,e:
print "Exception:\n",e
finally:
ret_file.close()

不過若要根據本身需求來作cmdb咱們的路還要往前走

相關文章
相關標籤/搜索