provider "aws" {
region = "ap-northeast-2"
access_key = "xxx"
secret_key = "yyy"
}
# terraform apply -auto-approve
#创建VPC
resource "aws_vpc" "tf-vpc-test"{
#定义ip块
cidr_block = "10.10.0.0/16"
#设置允许dns主机名
enable_dns_hostnames = true
#设置标签
tags = {Name = "my test vpc1"}
}
#创建internet网关,别名tf-gw-test,并附加到VPC
resource "aws_internet_gateway" "tf-igw-test"{
#绑定到vpc,${aws_vpc.test.id}为获取步骤2创建的vpc id
vpc_id = "${aws_vpc.tf-vpc-test.id}"
}
#创建子网,别名a_public
resource "aws_subnet" "a_public"{
#指定所属的VPC
vpc_id = "${aws_vpc.tf-vpc-test.id}"
#设置ip块
cidr_block = "10.10.1.0/24"
#设置可用区
availability_zone = "ap-northeast-2a"
#设置标签
tags = {Name = "Seoul Public-a"}
}
# #创建关联internet gateway的路由表
# resource "aws_route_table" "a_public"{
# #指定所属的VPC
# vpc_id = "${aws_vpc.tf-vpc-test.id}"
# #绑定internet gateway,并绑定到0.0.0.0/0
# route{
# cidr_block = "0.0.0.0/0"
# gateway_id = "${aws_internet_gateway.tf-igw-test.id}"
# }
# }
# #创建普通的路由表
# resource "aws_route_table" "a_private"{
# vpc_id = "${aws_vpc.tf-vpc-test.id}"
# }
# #关联子网和路由表
# resource "aws_route_table_association" "a_public"{
# #指定子网id
# subnet_id = "${aws_subnet.a_public.id}"
# #指定路由表id
# route_table_id = "${aws_route_table.a_public.id}"
# }
# 使用默认的路由表
resource "aws_default_route_table" "main-rtb"{
default_route_table_id = aws_vpc.tf-vpc-test.default_route_table_id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.tf-igw-test.id
}
tags = {
# Name: "${var.env_prefix}-main-rtb"
Name: "tf-test-main-rtb"
}
}
# 使用默认SG,安全组。直接在默认SG上修改策略。
resource "aws_default_security_group" "myapp-default-sg" {
# 默认SG名字是default
# name = "myapp-sg"
vpc_id = aws_vpc.tf-vpc-test.id
# 每个ingress是一个inbound rule,egress是一个出方向的rule。
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
# cidr_blocks 定义外部的源地址。
cidr_blocks = [ "1.1.1.0/24" ]
# 需要定义变量
# cidr_blocks = [ var.my_ip ]
}
ingress {
from_port = 0
to_port = 443
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}
egress {
from_port = 0
to_port = 0
# "-1"是任意协议
protocol = "-1"
# 任意地址[ "0.0.0.0/0" ]
cidr_blocks = [ "0.0.0.0/0" ]
prefix_list_ids = []
}
}
# # 自定义SG,安全组。
# # 与默认的SG只用 资源名 ,和 name 不一样,其他都一样。
# resource "aws_security_group" "myapp-sg" {
# name = "myapp-sg"
# vpc_id = aws_vpc.tf-vpc-test.id
# # 每个ingress是一个inbound rule,egress是一个出方向的rule。
# ingress {
# from_port = 22
# to_port = 22
# protocol = "tcp"
# # cidr_blocks 定义外部的源地址。
# cidr_blocks = [ "1.1.1.0/24" ]
# # 需要定义变量
# # cidr_blocks = [ var.my_ip ]
# }
# ingress {
# from_port = 0
# to_port = 443
# protocol = "tcp"
# cidr_blocks = [ "0.0.0.0/0" ]
# }
# egress {
# # 0是任意端口
# from_port = 0
# to_port = 0
# # "-1"是任意协议
# protocol = "-1"
# # 任意地址[ "0.0.0.0/0" ]
# cidr_blocks = [ "0.0.0.0/0" ]
# prefix_list_ids = []
# }
# }
import json
import requests
url = 'http://192.168.31.52/zabbix/api_jsonrpc.php'
post_headers = {'Content-Type': 'application/json'}
post_data = {
"jsonrpc" : "2.0",
"method" : "user.login",
"params" : {
"user" : "Admin",
"password" : "zabbix"
},
"id" : 1
}
ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
retdic = json.loads(ret.text)
user_cookie = retdic["result"]
##print(ret.text) #以字符串的形式显示所有属性
##print(user_cookie) #相当于浏览器获得的cookie
post_data = {
"jsonrpc": "2.0",
"method": "history.get",
"params": {
"output": "extend",
"history": 0,
"itemids": "23296",
"sortfield": "clock",
"sortorder": "DESC",
"limit": 10
},
"auth": user_cookie,
"id": 1
}
##ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
##print(ret.text['result'])
####查询所有主机信息
##post_data = {
## "jsonrpc": "2.0",
## "method": "host.get",
## "params": {
## "output": [
## "hostid",
## "host"
## ],
## "selectInterfaces": [
## "interfaceid",
## "ip"
## ]
## },
## "id": 2,
## "auth": user_cookie #这是第一步获取的身份验证令牌
##}
##
##ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
##print(ret.text)
####查询所有主机信息
##post_data = {
## "jsonrpc": "2.0",
## "method": "hostgroup.get",
## "params": {
## "output": "extend",
## "filter": {
## "name": [
## "192.168.31.1"
## ]
## }
## },
## "auth": user_cookie,
## "id": 1
##}
##
##ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
##print(ret.text)
####获取模版信息
##post_data = {
## "jsonrpc": "2.0",
## "method": "template.get",
## "params": {
## "output": "extend",
## "filter": {
## "host": [
## "Template OS Linux"
## ]
## }
## },
## "auth": user_cookie,
## "id": 1
##}
##
##ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
##print(ret.text)
##
##
####创建主机
##post_data = {
## "jsonrpc": "2.0",
## "method": "host.create",
## "params": {
## "host": "Linux server",
## "interfaces": [
## {
## "type": 1,
## "main": 1,
## "useip": 1,
## "ip": "192.168.3.1",
## "dns": "",
## "port": "10050"
## }
## ],
## "groups": [
## {
## "groupid": "2" #填写第3步获取的组ID
## }
## ],
## "templates": [
## {
## "templateid": "10001" #填写第4步获取的模板ID
## }
## ],
## "macros": [
## {
## "macro": "{$USER_ID}",
## "value": "123321"
## }
## ],
## "inventory_mode": 0,
## "inventory": {
## "macaddress_a": "01234",
## "macaddress_b": "56768"
## }
## },
## "auth": "dfba5d41dc9b46d6525f70af13631cb6",
## "id": 1
##}
##
##ret = requests.post(url, data = json.dumps(post_data), headers = post_headers)
##print(ret.text)
#获取历史值
##post_data2 = {
## "jsonrpc": "2.0",
## "method": "history.get",
## "params": {
## "output": "extend",
## "history": 0,
## "itemids": "23296",
## "sortfield": "clock",
## "sortorder": "DESC",
## "limit": 10
## },
## "auth": user_cookie,
## "id": 1
##}
##
##
##ret2 = requests.post(url, data = json.dumps(post_data2), headers = post_headers)
##print(ret2.text)
from pyzabbix import ZabbixAPI
#https://blog.csdn.net/weixin_43790276/article/details/90664236
#https://blog.csdn.net/xiaolong_4_2/article/details/80892370
#https://blog.csdn.net/weixin_33955681/article/details/89858265
#https://blog.csdn.net/honux5i/article/details/78612469
ZABBIX_SERVER = 'http://192.168.31.52/zabbix'
zapi = ZabbixAPI(ZABBIX_SERVER)
zapi.login('Admin', 'zabbix')
# 获取主机
host_list = zapi.host.get(
output="extend",
filter={"hostid":"1000"},
selectItems=["name","itemid"],
)
# 获取主机组
hostgroup_list = zapi.hostgroup.get(
output="extend",
filter={"groupid":"xxx"}
select Hosts=["name":"hostid"]
)
# 获取触发器
triggers = zapi.trigger.get(
output="extend",
selectHosts=['host'],
)
# 获取应用
application_list = zapi.application.get(
hostids='',
output="extend",
)
# 获取监控项
item_list = zapi.item.get(
hostids='',
applicationids='',
output="extend",
)
# 获取模板
template = zapi.template.get(
output="extend",
)
# 通过key来筛选数据
item_list = zapi.item.get(
hostids='',
search={
"key_": '',
},
output="extend",
)
===
###########################
基本语法:
config.read('example.ini',encoding="utf-8")
"""读取配置文件,python3可以不加encoding"""
options(section)
"""sections(): 得到所有的section,并以列表的形式返回"""
config.defaults()
"""defaults():返回一个包含实例范围默认值的词典"""
config.add_section(section)
"""添加一个新的section"""
config.has_section(section)
"""判断是否有section"""
print(config.options(section))
"""得到该section的所有option"""
has_option(section, option)
"""判断如果section和option都存在则返回True否则False"""
read_file(f, source=None)
"""读取配置文件内容,f必须是unicode"""
read_string(string, source=’’)
"""从字符串解析配置数据"""
read_dict(dictionary, source=’’)
"""从词典解析配置数据"""
get(section, option, *, raw=False, vars=None[, fallback])
"""得到section中option的值,返回为string类型"""
getint(section,option)
"""得到section中option的值,返回为int类型"""
getfloat(section,option)
"""得到section中option的值,返回为float类型"""
getboolean(section, option)
"""得到section中option的值,返回为boolean类型"""
items(raw=False, vars=None)
"""和items(section, raw=False, vars=None):列出选项的名称和值"""
set(section, option, value)
"""对section中的option进行设置"""
write(fileobject, space_around_delimiters=True)
"""将内容写入配置文件。"""
remove_option(section, option)
"""从指定section移除option"""
remove_section(section)
"""移除section"""
optionxform(option)
"""将输入文件中,或客户端代码传递的option名转化成内部结构使用的形式。默认实现返回option的小写形式;"""
readfp(fp, filename=None)
"""从文件fp中解析数据"""
##########################
生成configparser文件例
import configparser # 配置文件
config = configparser.ConfigParser()
"""生成configparser配置文件 ,字典的形式"""
"""第一种写法""" #定义了一个叫DEFAULT的section
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
"""第二种写法"""
config['bitbucket.org'] = {} #定义了一个叫bitbucket.org的section
config['bitbucket.org']['User'] = 'hg'
"""第三种写法"""
config['topsecret.server.com'] = {} #定义了一个叫topsecret.server.com的section
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes' #向DEFAULT section里追加元素
"""写入后缀为.ini的文件"""
with open('example.ini', 'w') as configfile:
config.write(configfile)
输出:
没有输出,但是会生成一个叫example.ini的配置文件,内容如下,
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes
[bitbucket.org]
user = hg
[topsecret.server.com]
host port = 50022
forwardx11 = no
############################
读取上面生成的配置文件例
import configparser # 配置文件
config = configparser.ConfigParser()
config.read("example.ini")
print("所有节点==>", config.sections()) #不包括DEFAULT
print("默认值的词典==>", config.defaults())
for item in config["DEFAULT"]:
print("DEFAULT下所有option==>", item)
print("bitbucket.org节点下所有option的key,包括默认option==>", config.options("bitbucket.org"))
print("输出元组,包括option的key和value", config.items('bitbucket.org'))
print("bitbucket.org下user的值==>", config["bitbucket.org"]["user"]) # 方式一
topsecret = config['bitbucket.org']
print("bitbucket.org下user的值==>", topsecret["user"]) # 方式二
print("判断bitbucket.org节点是否存在==>", 'bitbucket.org' in config)
print("获取bitbucket.org下user的值==>", config.get("bitbucket.org", "user"))
print("获取option值为数字的:host port=", config.getint("topsecret.server.com", "host port"))
输出:
所有节点==> ['bitbucket.org', 'topsecret.server.com']
默认值的词典==> OrderedDict([('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes')])
DEFAULT下所有option==> serveraliveinterval
DEFAULT下所有option==> compression
DEFAULT下所有option==> compressionlevel
DEFAULT下所有option==> forwardx11
bitbucket.org节点下所有option的key,包括默认option==> ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
输出元组,包括option的key和value [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
bitbucket.org下user的值==> hg
bitbucket.org下user的值==> hg
判断bitbucket.org节点是否存在==> True
获取bitbucket.org下user的值==> hg
获取option值为数字的:host port= 50022
###########################
删除配置文件section和option的实例(默认分组有参数时无法删除,但可以先删除下面的option,再删分组)
import configparser #配置文件
config = configparser.ConfigParser()
config.read("example.ini")
config.remove_section("bitbucket.org")
"""删除分组"""
config.remove_option("topsecret.server.com","host port")
"""删除某组下面的某个值"""
config.write(open('example.ini', "w"))
输出:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes
[topsecret.server.com]
forwardx11 = no
###########################
配置文件的修改实例
"""修改"""
import configparser
config = configparser.ConfigParser()
config.read("example.ini")
config.add_section("new_section")
"""新增分组"""
config.set("DEFAULT","compressionlevel","110")
"""设置DEFAULT分组下compressionlevel的值为110"""
config.write(open('example.ini', "w"))
输出:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 110
forwardx11 = yes
[topsecret.server.com]
forwardx11 = no
[new_section]
Share
于 2021-04-04 21:28:11 首次发布