Centos 7.7 (1908) de Python 2.7 ile Oracle db ye baglanti saglanmasi.

1 – Önce oracle client yüklenir. Sonra aşağıdaki komutlar çalıştırılır;

yum install libaio
echo "/usr/lib/oracle/11.2/client64/lib" >/etc/ld.so.conf.d/oracle.conf
ldconfig
yum install epel-release
yum install gcc python-pip.noarch python-devel
pip install cx_Oracle

2 – Eğer son satırdaki “pip” paketi yüklü değilse hata verecektir. Paketi aşağıdaki gibi yüklüyoruz;

yum install gcc openssl-devel bzip2-devel
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
python2.7 get-pip.py
pip install cx_Oracle

Python ile oracle db ye bağlantının test edilmesi:

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import cx_Oracle
conn_str = "PROXXX/PROXXX@192.168.1.XXX:1521/XX"
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
c.execute("SELECT * FROM PRO_XXX_RESERVATIONINFO WHERE ROWNUM <= 10")
for row in c:
    print row[0], "-", row[1], "-", row[2], "-", row[3], "-", row[4]
conn.close()

Kaynaklar:

1 – https://tecadmin.net/install-python-2-7-on-centos-rhel/

2 – https://www.mylinuxplace.com/install-cx_oracle-on-centos-7/

Zabbixte (3.4.8, Ubuntu) sms entegrasyonu

1 – Öncelikle http://www.smsvitrini.com dan bir hesap açıp kullanıcı adı ve şifremizi alıyoruz.  Yanında 1.000 kredi alıyoruz.

2 – Sonra sms.sh adında bir script yazıp içine aşağıdaki kodları ekliyoruz. Bu dosyayı “/usr/lib/zabbix/alertscripts/sms.sh” da oluşturuyoruz.

_____________________

to=$1
subject=$2
mesaj=$3
curl –data “islem=1&user=<KULLANICI_ADI>&pass=<SIFRE>&mesaj=$mesaj&numaralar=$to&baslik=SMS_BASLIGI” http://api.smsvitrini.com/index.php

_____________________

3 – “chmod 755 sms.sh” yapıyoruz.

4- Zabbix’te  Administration > MediaType  kısmına “netsms” ekleyip aşağıdaki gibi giriyoruz;

zabbix sms ayar

5 – Artık “netsms” eklendi. Şimdi bir tane “Action” ekleyelim;

Yeni bir Action oluşturup “Operations” kısmından aşağıdaki ekrandaki gibi “netsms” seçilir ve update yapılır;

zabbix sms action

6 – Artık event oluştuğunda ilgili trigger “netsms”i çalıştırıp sms gönderecektir. Event oluşmadan sms in gidip gitmeyeceğini terminalden aşağıdaki komutu vererek test edebiliriz;

curl –data “islem=1&user=<KULLANICI_ADINIZ>&pass=<SIFRENIZ>&mesaj=deneme 123&numaralar=<CEP_NUMARANIZ>&baslik=<SMS_BASLIGINIZ>” http://api.smsvitrini.com/index.php

Komut sonrasında “DONUS|OK:75248541” gibi bir mesaj aldıysanız sms gitmiştir. Bunun dışında bir yerde hata vardır.

 

Zabbix (3.4.8,Ubuntu) üzerinde Python’dan MSSQL’e bağlantı için pyodbc kurulumu ve test edilmesi.

MSSQL PYODBC kurulumu:
$ git clone https://github.com/mkleehammer/pyodbc.git (Buradaki dosyayı indir ve kok e at, unzip et. oncesinde git yoksa yukle (apt-get install -y git))
$ python3 setup.py build
# python3 setup.py install
Sonrasın test edilmesi (mssql.py);
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import pyodbc
################## TANIMLAR #####################
dsn = ‘MSSQLServer’
user = ‘sa’
password = ‘XXXXXX’
database = ‘MCXXXPOS’
################## TANIMLAR #####################
def kullanici_liste():
con_string = ‘DSN=%s;UID=%s;PWD=%s;DATABASE=%s;’ % (dsn, user, password, database)
con = pyodbc.connect(con_string)
cur = con.cursor()
cur.execute(“SELECT e.firstname, e.lastname,e.posref FROM LOCATION_XXXXX_DB.dbo.EMPLOYEE e”)
while (1):
row = cur.fetchone ()
if row == None:
break
print((‘%s %s ‘ %(row[1],row[2])))
kullanici_liste()
_________________________________________________________________________________________________
Not : çalıştırınca hata alırsan, dsn tanımlamasını yapmalısın.
/etc/odbc.ini dosyasından aşağıdakiler ekle;
[MSSQLServer]
Driver = FreeTDS
Description = Any description
Trace = No
Server = 192.168.XXX.XXX
Port = 1433
TDS version = 0.95
Database = MCXXXPOS

Zabbix (3.4.8,Ubuntu) üzerinde Python’dan Oracle’a bağlantı için cx_Oracle kurulumu ve test edilmesi.

cx_oracle kurulumu
1 – pip kurulumu yapılır:
sudo apt-get install python3-pip python3-dev build-essential
sudo pip install –upgrade pip
sudo pip install –upgrade virtualenv
2 – pip install cx_Oracle

python ile test (oracle.py):

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import cx_Oracle

conn_str = “PROXXX/PROXXX@192.168.XXX.XXX:1521/VX8”
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
c.execute(“SELECT PG.ADI, PG.SOYADI, PG.VERILENODANO, PG.GELISTARIHI, PG.AYRILISTARIHI FROM PRO_XXX PG ORDER BY PG.VERILENODANO”)
for row in c:
print (row[0], ‘-‘, row[1], ‘-‘, row[2], ‘-‘, row[3], ‘-‘, row[4])
conn.close()

Zabbix (3.4.8,Ubuntu) üzerine FreeTDS ile MSSQL’e bağlantı kurulumu, php ile test edilmesi.

MSSQL FREETDS kurulum notları:
1 – sudo apt-get install -y unixodbc unixodbc-dev unixodbc-bin libodbc1 odbcinst1debian2 tdsodbc php7.0-odbc
2 – sudo apt-get install -y freetds-bin freetds-common freetds-dev libct4 libsybdb5
3 – sudo nano /etc/odbcinst.ini ve aşağıdakileri ekle;
[ODBC]
Trace = No
TraceFile = /tmp/odbc.log
[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
UsageCount = 1
4 – sudo nano /etc/odbc.ini ve aşağıdakileri ekle;
[dbserverdsn]
Driver = FreeTDS
Server = 192.168.XXX.X51
Port = 1433
Database = <MC…POS>
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
UsageCount = 1
[Default]
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
5 – sudo nano /etc/freetds/freetds.conf açılır ve dosya içeriği aşağıdaki gibi yapılır;
_________________________________________________________________________________________________
#   $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage “man freetds.conf”.
# Global settings are overridden by those in a database
# server specific section
[global]
        # TDS protocol version
tds version = 4.2
port = 1433
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting ‘text size’ to a more reasonable limit
text size = 64512
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 5.0
# A typical Microsoft server
[dbserverdsn]
database = <MC…POS>
host = 192.168.XXX.x51
port = 1433
tds version = 7.0
client charset = UTF-8
_________________________________________________________________________________________________
kaydedilir ve çıkılır.
6 – PHP test sayfası :

<?php

ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(E_ALL);

$baglanti = odbc_connect(“dbserverdsn”,”sa”,”user_sifre”);

if ($baglanti) echo “<span style=’color:green’>Baglandi</span>”.”</br>”;

$sorgu = odbc_exec($baglanti,”SELECT e.firstname, e.lastname,e.posref FROM LOCATION_ACTIVITY_DB.dbo.XXXXXXX e”);

while( $row = odbc_fetch_array($sorgu) ){
echo $row[“firstname”].” “.$row[“lastname”].”<br>”;
}
?>

7 – Başka DB lere bağlanılacaksa /etc/odbc.ini dosyasına eklenip bağlantı yapılabilir. Örnek;
sudo nano /etc/odbc.ini
[xxx_pos]
Driver = FreeTDS
Server = 192.168.XXX.X51
Port = 1433
Database = <MC..POS>
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
UsageCount = 1
[yyy_pos]
Driver = FreeTDS
Server = 192.168.XXX.X51
Port = 1433
Database = <DB_ISMI>
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
UsageCount = 1
Not : Başka db ye bağlanırken o db yi sorguda belirtilmelidir.
Örnek :
$baglanti = odbc_connect(“xxx_pos”,”sa”,”user_sifre”);

veya

$baglanti = odbc_connect(“yyy_pos”,”sa”,”user_sifre”);
gibi…
8 – “service apache2 restart” komutuyla apache restart edilir ve işlem bitti…

Zabbix (3.4.8,Ubuntu) üzerine Oracle 11.2 için “Oracle Client” kurulumu, php ile test edilmesi.

Oracle client kurulumu

Önce buradan basic, devel ve sqlplus rpm paketleri indirilir. Sonra alttaki adımlardan devam edilir.

1 – Zabbix e WinSCP ile bağlanıp home klasöre oracle dosyaları atılır. (.rpm dosyaları)
2 – Sonra apt-get update & apt-get install -y gcc ntp alien unzip php7.0-dev kurulur.
3 – Restart önerilir : shutdown -r now
4 – kok teki oracle dosyaları (rpm ler) alien -i oracle-basic,devel ve sqlplus olarak TEKER TEKER kurulur.
5 – sqlplus PROXXX/PROXXX@192.168.xxx.xxx/VX8 veya sqlplus64 PROxxx/PROxxx@192.168.xxx.xxx/Vx8 komutlarıyla test edildiğince eğer hata alınıyorsa,
5.1 export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} komutu uygulanır.
5.2 sudo nano /etc/profile.d/oracle.sh && sudo chmod o+r /etc/profile.d/oracle.sh ve içine
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export PATH=$PATH:$ORACLE_HOME/bin
satırları girilir.
5.3 sudo nano /etc/ld.so.conf.d/oracle.conf && sudo chmod o+r /etc/ld.so.conf.d/oracle.conf && ldconfig
ve içine
/usr/lib/oracle/11.2/client64/lib/
yazılır.
6 – Restart önerilir : shutdown -r now

Sonrasında OCI kurulumu :
1 – Dosya kok e atılır oci8-2.1.8.tgz (öncesinde bu dosya internetten indirilir)
2 – tar -xzvf oci8-2.1.8.tgz ile açılır
3 – cd oci8-2.1.8 ile klasore girilir
4 – phpize yapılır
5 ./configure –with-oci8=shared,instantclient,/usr/lib/oracle/11.2/client64/lib
6 – make yapılır
7 – make install yapılır.
8 – /usr/lib/php/20151012/oci8.so da dosyanın olduğu kontrol edilir.
9 – /etc/php/7.0/cli/php.ini ve /etc/php/7.0/apache2/php.ini açılır ve [OCI8] yazan yerin altına “extension=oci8.so” eklenir
10 – “service apache2 restart” yapılır.
11- VE KURULUM BİTTİ… 🙂

PHP de test edip kontrol :

<?php

ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(E_ALL);

$conn = oci_connect(‘PROXXX’, ‘PROXXX’, ‘//192.168.XXX.XXX:1521/VX8’);

$stid = oci_parse($conn, ‘SELECT PG.ADI, PG.SOYADI, PG.VERILENODANO, PG.GELISTARIHI, PG.AYRILISTARIHI FROM XXXXXX PG’);
oci_execute($stid);

echo “<table>\n”;
while (($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
echo “<tr>\n”;
foreach ($row as $item) {
echo ”  <td>”.($item !== null ? htmlentities($item, ENT_QUOTES) : “&nbsp;”).”</td>\n”;
}
echo “</tr>\n”;
}
echo “</table>\n”;

?>