AKAI TSUKI

System development or Technical something

try to use zabbix api on bash

I created this script to add host/host group by Zabbix API.

#!/bin/bash
set -x

### config
user_name=user01
user_pass=pass_string
zabbix_url=http://localhost/zabbix/api_jsonrpc.php

header="Content-Type: application/json-rpc"


# ------------------------------------------
# set token
# ------------------------------------------
function set_token() {

local json_data=$(cat << EOS
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "${user_name}",
        "password": "${user_pass}"
    },
    "id": 1,
    "auth": null
}
EOS
)

token=`curl -s -H "${header}" ${zabbix_url} -d "${json_data}" | jq -r .result`

}


# ------------------------------------------
# get host
# ------------------------------------------
function get_host() {

json_data=$(cat << EOS
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
      "output": [
        "hostid",
        "host"
      ],
      "selectInterfaces": [
        "interfaceid",
        "ip"
      ]
    },
    "id": 2,
    "auth": "${token}"
}
EOS
)

result=`curl -s -d  ${json_data} -H "${header}" ${zabbix_url} | jq -r .result`

}


# ------------------------------------------
# add host group
#   $1 - host group name
# ------------------------------------------
function add_host_group() {

local json_data=$(cat << EOS
{
    "jsonrpc": "2.0",
    "method": "hostgroup.create",
    "params": {
        "name": "$1"
    },
    "auth": "${token}",
    "id": 3
}
EOS
)

# echo "${json_data}"
ret_value=`curl -s -H "${header}" ${zabbix_url} -d "${json_data}" | jq -r .result.groupids[0]`

}


# ------------------------------------------
# get host group
#   $1 - host group name
# ------------------------------------------
function get_host_group() {

local json_data=$(cat << EOS
{
    "jsonrpc": "2.0",
    "method": "hostgroup.get",
    "params": {
        "output": "extend",
        "filter": {
            "name": [
                "$1"
            ]
        }
    },
    "auth": "${token}",
    "id": 4
}
EOS
)

# echo "${json_data}"
ret_value=`curl -s -H "${header}" ${zabbix_url} -d "${json_data}" | jq -r .result[0].groupid`
echo $ret_value

}


# ------------------------------------------
# add host
#   $1 - host name
#   $2 - host group id
#   $3 - ip
# ------------------------------------------
function add_host() {

local json_data=$(cat << EOS
{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "$1",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "$3",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "$2"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ]
    },
    "auth": "${token}",
    "id": 5
}
EOS
)

# echo "${json_data}"
ret_value=`curl -s -H "${header}" ${zabbix_url} -d "${json_data}" | jq .result`
echo $ret_value

}


# ------------------------------------------
# MAIN
# ------------------------------------------

set_token
echo $token

# add group
group_name="test group33"
get_host_group "${group_name}"

if [ "null" = "${ret_value}" ]; then
  # add host group
  add_host_group "${group_name}"
  group_id=${ret_value}

else
  # already added
  group_id=${ret_value}

fi

echo ${group_id}

# add host
host_name="test_host1"
ip_address="192.168.100.10"
add_host "${host_name}" "${group_id}" "${ip_address}"

openstack bash complete

bash complete

https://docs.openstack.org/python-openstackclient/pike/cli/command-objects/complete.html

[root@localhost ~(keystone_admin)]# openstack complete | sudo tee /etc/bash_completion.d/osc.bash_completion > /dev/null
[root@localhost ~(keystone_admin)]# ls -l /etc/bash_completion.d/
total 196
-rw-r--r-- 1 root root    955 Aug  2  2018 cinder.bash_completion
-rw-r--r-- 1 root root    882 Dec 12 12:53 glance
-rwxr--r-- 1 root root  11345 Oct 13  2017 gluster
-rw-r--r-- 1 root root    829 Oct 31 04:20 iprutils
-rw-r--r-- 1 root root    885 Jul 27  2018 nova
-rw-r--r-- 1 root root 100219 Apr 30 16:09 osc.bash_completion
-rwxr-xr-x 1 root root  17838 Feb 14 18:58 ovs-appctl-bashcomp.bash
-rwxr-xr-x 1 root root  28733 Feb 14 18:58 ovs-vsctl-bashcomp.bash
-rw-r--r-- 1 root root   1458 Nov 26  2013 redefine_filedir
-rw-r--r-- 1 root root  11272 Oct 31 07:58 yum-utils.bash
[root@localhost ~(keystone_admin)]#

network setting for openstack

After installation of OpenStack, akai-tsuki.hatenablog.com

Network setting

I use network 1 to access to VM in OpenStack.
and I connect to server via network 2 by ssh.

f:id:akai_tsuki:20190503173937p:plain
all_in_one_network

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls -ltr ifcfg-*
-rw-r--r-- 1 root root 254 Aug 24  2018 ifcfg-lo
-rw-r--r-- 1 root root 329 Apr 30 13:41 ifcfg-enp0s8
-rw-r--r-- 1 root root 424 Apr 30 14:42 ifcfg-enp0s3
-rw-r--r-- 1 root root 192 Apr 30 14:47 ifcfg-br-ex
[root@localhost network-scripts]#
[root@localhost network-scripts]# cat ifcfg-br-ex
DEVICE=br-ex
DEVICETYPE=ovs
TYPE=OVSBridge
BOOTPROTO=static
NM_CONTROLLED=no
ONBOOT=yes
HOTPLUG=no
IPADDR=172.16.10.152
PREFIX=24
GATEWAY=172.16.10.1
DNS1=172.16.10.1
DEFROUTE=yes
IPV6INIT=no
[root@localhost network-scripts]#
[root@localhost network-scripts]# cat ifcfg-enp0s3 | grep -v "^#"
NM_CONTROLLED=no
DEVICETYPE=ovs
TYPE=OVSPort
OVS_BRIDGE=br-ex
DEVICE=enp0s3
NAME=enp0s3
BOOTPROTO=none
UUID=3a0633b6-815b-4196-9756-6824a53b07ff
ONBOOT=yes
[root@localhost network-scripts]#
[root@localhost network-scripts]# cat ifcfg-enp0s8 | grep -v "^#"
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.20.10
PREFIX=24
DEFROUTE=no
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s8
UUID=054f6482-6dec-4d83-bf71-0c7b050beb79
DEVICE=enp0s8
ONBOOT=yes
[root@localhost network-scripts]#
[root@localhost network-scripts]# ip a s br-ex
7: br-ex: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    link/ether 08:00:27:95:13:e8 brd ff:ff:ff:ff:ff:ff
    inet 172.16.10.152/24 brd 172.16.10.255 scope global br-ex
       valid_lft forever preferred_lft forever
    inet6 fe80::bc97:d4ff:fedf:9f46/64 scope link
       valid_lft forever preferred_lft forever
[root@localhost network-scripts]#

[root@localhost network-scripts]# ip a s enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master ovs-system state UP group default qlen 1000
    link/ether 08:00:27:95:13:e8 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::a00:27ff:fe95:13e8/64 scope link
       valid_lft forever preferred_lft forever
[root@localhost network-scripts]#

[root@localhost network-scripts]# ip a s enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:ba:eb:1f brd ff:ff:ff:ff:ff:ff
    inet 192.168.20.10/24 brd 192.168.20.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:feba:eb1f/64 scope link
       valid_lft forever preferred_lft forever
[root@localhost network-scripts]#
[root@localhost network-scripts]# ovs-vsctl list-br
br-ex
br-int
br-tun
[root@localhost network-scripts]#

[root@localhost network-scripts]# ovs-vsctl list-ports br-ex
enp0s3
phy-br-ex
[root@localhost network-scripts]#

install openstack by packstack in CentOS 7

prepare

[root@localhost ~]# yum install centos-release-openstack-rocky.noarch
[root@localhost ~]# cat /etc/environment
LANG=en_US.utf-8
LC_ALL=en_US.utf-8
[root@localhost ~]#
[root@localhost ~]# yum update -y
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable NetworkManager
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
Removed symlink /etc/systemd/system/network-online.target.wants/NetworkManager-wait-online.service.
[root@localhost ~]# systemctl stop NetworkManager
[root@localhost ~]# systemctl enable network
network.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig network on
[root@localhost ~]# systemctl start network
[root@localhost ~]#

install

[root@localhost ~]# yum install -y openstack-packstack
[root@localhost ~]# packstack --allinone

try dnsmasq

Install Dnsmasq

[root@cent7devops ~]# yum -y install dnsmasq

initial state after yum install

[root@cent7devops ~]# grep -v -e "^#.*" -e "^$" /etc/dnsmasq.conf
conf-dir=/etc/dnsmasq.d,.rpmnew,.rpmsave,.rpmorig
[root@cent7devops ~]#
[root@cent7devops ~]# systemctl status dnsmasq
● dnsmasq.service - DNS caching server.
   Loaded: loaded (/usr/lib/systemd/system/dnsmasq.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@cent7devops ~]#
[root@cent7devops ~]# ls -l /etc/dnsmasq.conf
-rw-r--r-- 1 root root 26832 Apr 11 09:53 /etc/dnsmasq.conf
[root@cent7devops ~]#
[root@cent7devops ~]# ls -l /etc/dnsmasq.d/
total 0
[root@cent7devops ~]#

Configure 'dnsmasq.conf' and hosts file

[root@cent7devops ~]# diff /etc/dnsmasq.conf /etc/dnsmasq.conf.org
19c19
< domain-needed
---
> #domain-needed
21c21
< bogus-priv
---
> #bogus-priv
53c53
< strict-order
---
> #strict-order
[root@cent7devops ~]#
[root@cent7devops ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.10.111 gitlab.node01.devlocal node01
172.16.10.112 node02.devlocal node02
172.16.10.113 node03.devlocal node03
172.16.10.90 devops.devlocal devops
[root@cent7devops ~]#

and Start Dnsmasq

[root@cent7devops ~]# systemctl start dnsmasq
[root@cent7devops ~]#
[root@cent7devops ~]# systemctl is-active dnsmasq
active
[root@cent7devops ~]#

operation check

confirm to access Dnsmasq. In this case, "172.16.10.90" is the IP Address of server running Dnsmasq.

[root@localhost ~]# nmcli -t -f ipv4.dns c s enp0s3
ipv4.dns:172.16.10.90
[root@localhost ~]#

[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 172.16.10.90
[root@localhost ~]#

The hosts file in server for test have only localhost.

[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]#

Try to execute dig command.

[root@localhost ~]# dig node02 +noall +answer

; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7_5.1 <<>> node02 +noall +answer
;; global options: +cmd
node02.                 0       IN      A       172.16.10.112
[root@localhost ~]#
[root@localhost ~]# dig node03 +short
172.16.10.113
[root@localhost ~]#

I try to use opetion of "nmcli" command.

option "-t" and "-f"

ex. 1

[root@localhost ~]# nmcli -f ipv4.addresses c s enp0s3
ipv4.addresses:                         172.16.10.111/24
[root@localhost ~]#

ex. 2

[root@localhost ~]# nmcli -t -f ipv4.addresses c s enp0s3
ipv4.addresses:172.16.10.111/24
[root@localhost ~]#

ex. 3

[root@localhost ~]# nmcli -t -f ipv4 c s enp0s3
ipv4.method:manual
ipv4.dns:172.16.10.90
ipv4.dns-search:
ipv4.dns-options:
ipv4.dns-priority:0
ipv4.addresses:172.16.10.111/24
ipv4.gateway:172.16.10.1
ipv4.routes:
ipv4.route-metric:-1
ipv4.route-table:0
ipv4.ignore-auto-routes:no
ipv4.ignore-auto-dns:no
ipv4.dhcp-client-id:
ipv4.dhcp-timeout:0
ipv4.dhcp-send-hostname:yes
ipv4.dhcp-hostname:
ipv4.dhcp-fqdn:
ipv4.never-default:no
ipv4.may-fail:yes
ipv4.dad-timeout:-1
[root@localhost ~]#

option "-g"

ex. 1

[root@localhost ~]# nmcli -g connection.id c s enp0s3
enp0s3
[root@localhost ~]#

ex. 2

[root@localhost ~]# nmcli -g ipv4.addresses c s enp0s3
172.16.10.111/24
[root@localhost ~]#