Tuesday, July 16, 2019

YAML

YAML Ain't Markup Language:
  1. YAML uses indentation to distinguish layers (same indentation - same layer) - use spaces, because tabs are not allowed.
  2. YAML start with three dashes: ---
  3. YAML disctionary in key-value pair in one of two forms:
    1. colon separated key (like Python dictionary):
      1. key: value
    2. indentation separated key:
      1. key:
      2.       value
    3. Also one key can contain nested dictionary:
      1. Method 1:
        1. first_level_key:
        2.    second_level_key_under_the_first_level: second_level_value
      2. Method 2:
        1. first_level_key: {second_level_key_under_the_first_level: second_level_value}
  4. YAML uses dashes as indentation to represent list of items (use lists when you want key to have more than one values which are not keys themselves):
    1. Methos 1:
      1. this_is_a_list:
      2.  - element_1
      3.  - element_2
    2. Method 2:
      1. this_is_a_list : [element_1, elemenet_2]

Relational Algebra

RA is a formal language that forms conventions used in implemented languages like SQL.
RA operates on relations and produces relations as result.
Query (expression) on set of relations produces relation as result. 
Key is an attribute or a set of attributes whose value is guaranteed to be unique.
Tuple  - row of a relation.
Attribute  - column of a relation.
Relation - table consisting of attribute (column) and tuples (rows).
Schema  - relation header (attribute names)
We'll use simple college admission database with three relations (keys are in bold):
  1. 1st relation: College(schema: cName, state, enrollment)
  2. 2nd relation: Student(schema: sID, sName, GPA, sizeHS) # sizeHS = size of High School a student attended
  3. 3rd relation: Apply(schema: sID, cName, major, decision)
Simplest query in a RA is simply the relation name, for example Student is valid expression (query) in RA and it's returning copy of Student relation.
Use operators to filter, slice, combine relations:
  1. Select - picks certain rows out of a relation - σ (sigma):
    1. Students with GPA > 3.7 (GPA - Grade Point Average)
      1. σ GPA > 3.7 Student
    2. Students with GPA > 3.7 and sizeHS < 1000 (caret ^ is logical and operator)
      1. σ GPA > 3.7  ^ sizeHS < 1000 Student
    3. Application for Stanford for CS major
      1. σ cName="Stanford" ^ major="CS" Apply
    4. Select operator general case:
      1. σ condition Relation
  2. Project operator - picks certain columns:
    1. Apply relation with sID and decision columns only
      1. П sID,decision Apply
    2. Project operator general case:
      1. П col1,col2,col3... Relation
  3. To Select and Project at the same time:
    1. ID and name of students with GPA>3.7:
      1. П sID,sName (σ GPA>3.7 Student)
  4. Duplicates:
    1. SQL is based on multisets/bags, so duplicates are also shown
    2. RA is based on sets, so duplicates are eliminated automatically
  5. Cross-product operator (a.k.a. cross-join) - combine two relations (a.k.a. Cartesian product) horizontally:
    1. Student x Apply as result we get big relation which going to have eight (8) attributes
    2. as a convention when cross-product is done and we get the same attributes for both cross-producted relations we preface their name with the name of the relation they came from: Student.sID / Apply.sID
    3. cross-product gives as relation where for every row of Student relation you get all rows of Apply relation
    4. Names and GPA's of students with HS>1000 who applied to CS and were rejected:
      1.  П sName, GPA (σ Student.sID=Apply.sID ^ HS>1000 ^ major="CS" ^ decision="Reject" (Student x Apply))
  6. Difference operator A\B or A-B returns elements of A that not in B:
    1. if A = {a, b, c} and B = {b, c, d} then A - B = {a}
    2. IDs of students who didn't apply anywhere:
      1.  sID Student) - (П sID Apply)
    3. IDs and names of students who didn't apply anywhere (we can't just add sName to the project from Student relation because Apply relation has not this attribute and we might not use difference operator on sets with different quantity of attributes):
      1. П sID,sName ( ( (П sID Student) - (П sID Apply) ) ⋈ Student )
  7. Union (denoted by ∪) operator is the set of all elements in the collection. It is one of the fundamental operations through which sets can be combined and related to each other. Union combines vertically:
    1. A ∪ B means all members of A and also all members of B that not in A (RA is set based and removes duplicates):
      1. if A = {a, b, c} and B = {b, c, d} then:
        1. all members of A are {a, b, c} 
        2. all members of B that not in A is B\A = {d}
        3. combine both {a, b, c} and {d} => {a, b, c, d}
        4. so: A ∪ B = {a, b, c, d}
    2. List of college and student names:
      1.  cName College) ∪ (П sName Student)
  8. Natural join operator performs cross-product operator and then enforces equality on all of the attributes with the same name (as in above example: Student.sID=Apply.sID) also natural join eliminates one copy of duplicate attributes:
    1. Names and GPA's of students with HS>1000 who applied to CS and were rejected:
      1. П sName, GPA (σ HS>1000 ^ major="CS" ^ decision="Reject"(Student ⋈ Apply))
    2. Names and GPA's of students with HS>1000 who applied to CS and were rejected to colleges with the enrollment greater than 20000:
      1. П sName, GPA (σ HS>1000 ^ major="CS" ^ decision="Reject" ^ enrollment>20000(Student ⋈ (Apply ⋈ College)))
    3. Relation between ⋈ and x:
      1. E1 ⋈ E2 => П schema(E1) U schema(E2) (σ E1.a1= E2.a1 ^ E1.a2= E2.a2 ^ ... (E1 x E2))
  9. Theta Join - operator takes two expressions/relations and combines them with bow tie looking operator (⋈) but with a subscript theta (θ) which means select condition (any select condition you want):
    1. E1 ⋈θE2 = σθ (E1 ⋈ E2 )
    2. Theta join is the basic operation implemented in RDBMS so the term "join" often means theta-join
  10. Intersection operator -  A ∩ B of two sets A and B is the set that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements:
    1. if A = {1, 2, 3} and B = {2, 3, 4} then A ∩ B = {2, 3}
    2. Names that are both college name and student name:
      1.  sName Student) ∩ (П cName College)
    3. Expressing intersection via difference
      1. A ∩ B => A - ( A - B ) 
    4. Expressing intersection via natural join:
      1. A ∩ B => A ⋈ B
  11. Rename operator uses ρ (rho), it reassigns schema in the result of expression (relation). Above (in 7.2.1 and 10.2.1 we used operators on relations with different attribute names - different schemas, in practice RA doesn't allow that, so we need to use rename operator):
    1. General form:
      1. ρ R(A1,A2,...An) Relation E # call result of the relation E - R with attributes A1 to An
    2. Unify schemas for set operators:
      1. List of college and student names (7.2.1):
        1. ρ C1(name) (П cName College) ∪ ρ C2(name)  sName Student)
      2. Names that are both college name and student name (10.2.1):
        1. ρ C1(name) (П sName Student) ∩ ρ C2(name) (П cName College)
    3. for disambiguation in self-joins:
      1. pairs of colleges in same state:
        1. With cross-join
          1. σ s1=s2 ^ n1!=n2 ( ρ C1(n1,s1,e1) College x ρ C2(n2,s2,e2) College )
        2. With natural-join:
          1. σ n1!=n2 ( ρ C1(n1,s,e1) College x ρ C2(n2,s,e2) College )
  12. Select, Project, Cross-join, union, difference, rename are RA basic operators
  13. Natural join, theta join, intersection are not RA basic operators they can be expressed with use of basic operators, so there are actually are abbreviations

These materials were used to write this synopsis-post:

Wednesday, July 10, 2019

Hash Functions, Binary Tree, O(n)

Hash Function

A hash function is any function that can be used to map data of arbitrary size to data of fixed size (N).  Example of simple hash function is h(x) = x mod N (mod is modulus or remainder of division, for example 5 / 5 = 1 with no reminder => mod=0, 6 / 5 = 1 and 1 in remainder => mod = 1)The values returned by a hash function are called hash values, hash codes, digests, or simply hashes. Hash functions are often used in combination with a hash table (consists of hash function h and array, also called table, of size N), a common data structure used in computer software for rapid data lookup. Hashing is done for indexing and locating items in databases because it is easier to find the shorter hash value than the longer string. Hashing is also used in encryption.This term is also known as a hashing algorithm or message digest function. No sorting and no searching required. When you compute the hash function you know where to store the data and you know where to find the data. Hash functions are just one-way they cannot be reversed. The main idea is to store key-element pairs (k, e) as index h(k):
Example:
  1. phone book with 5 numbers in it (N = 5)
  2. h (name) = (lenght of name) mod 5
  3. This function is ok if all names in phonebook have different lengths, if some lengths are the same, then collision is occurred (collision - when pairs of input to hash function are mapped to the same hash value): 
    1. h(John) = 3 mod 5 = 3
    2. h(Jack) = 3 mod 5
    3. So h(John) = h(Jack)
    4. so because of many collision this is example of the bad hash-function
    5. actually collision can be in every hash function but hash function must be designed in the way minimizing collision possibility. To do so hash functions produce long enough hash-values and this values are hold smaller enough to be computed quickly.

Binary Tree

In computer science, a binary tree is a tree data structure in which each node (узел) has at most two children, which are referred to as the left child and the right child. Topmost node called root and this is L-0 (level zero) and height of 0. Each child node in binary tree defines a sub-tree, the root of which it is.

Big O notation

In computer science, big O notation is used to classify algorithms according to how their running time or space requirements grow as the input size grows.Actual formula is O(f(n)) meaning: with an increase in the parameter n (amount of input to the algorithm) the running time of the algorithm will increase no faster than some constant multiplied by f(n). How to find big-O of some operation (as Example 3n^2+ n^5 + 4n + 5 + 2^n + log8(n) ):
  1. omit constants and constant multipliers (3n^2+ n^5 + 4n + 5 + 2^n + log8(n) => n^2+ n^5 + n + 2^n + log8(n))
  2. n^a grows faster than n^b for a > b. In other words if you have n^3 - omit n^2 (n^2+ n^5 + n + 2^n + log8(n) => n^5 + 2^n + log8(n) )
  3. any polynomial grows faster than any logarithm, so n or even sqrt(n), grows faster than log3(n)  ( n^5 + 2^n + log8(n) => n^5 + 2^n )
  4. any exponential grows faster than any polynomial, so 3^n grows faster than n^5 ( n^5 + 2^n  => 2^n )
  5. So O(3n^2+ n^5 + 4n + 5 + 2^n + log8(n)) = 2^n
  6. All of the above doesn't mean that nobody cares constant - in practice speeding-up algorithm twice can be very hard but efficient, but it's much more reasonable to find approximate values first

Monday, July 8, 2019

Draft: Relax-and-Recover

Backup to USB and restore from USB

sudo yum install git syslinux syslinux-extlinux kernel-devel
git clone https://github.com/rear/rear.git
cd rear/
insert USB stick to the backed-up computer
lsblk # to find name of the USB flash card
umount /dev/sdb1 # umount if USB flash is automatically mounted
sudo usr/sbin/rear format /dev/sdb
type 'Yes' to format USB flash
rear will format that flash as REAR-000
edit rear configuration:
vi etc/rear/local.conf
### write the rescue initramfs to USB and update the USB bootloader
OUTPUT=USB
### create a backup using the internal NETFS method, using 'tar'
BACKUP=NETFS
### write both rescue image and backup to the device labeled REAR-000
BACKUP_URL=usb:///dev/disk/by-label/REAR-000
Create rescue image  (it's without OS backup and used to restore OS in case of failure) with verbose output:
sudo usr/sbin/rear -v mkrescue
Now reboot your system and try to boot from the USB device. If it's ok, then rescue image is ok and you can do OS data backup alonh with creating rescue media:
sudo usr/sbin/rear -v mkbackup




Monday, June 10, 2019

Access CentOS from the other machine (via VNC)

On target machine

yum groupinstall "GNOME Desktop"
Set default target to GUI:
systemctl set-default graphical.target
yum install tigervnc-server xorg-x11-fonts-Type1
Choose a VNC display to use for accessing this particular computer (here we'll use display 5 which equals to the port 5905)
cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:5.service
Add user (if you need one) whose Desktop will be accessed:
adduser admin
passwd admin
Access file:
vi /etc/systemd/system/vncserver@:5.service
Then:
  1. Go to [Service]
  2. ExecStart  - here - change <USER> with admin
  3. PIDFile - here - change <USER> with admin
firewall-cmd --permanent --zone=public --add-port=5905/tcp 
firewall-cmd --reload

su - admin
Execute vncserver command to assign VNC password for needed user (admin in our case)

systemctl daemon-reload 
systemctl start vncserver@:5.service
systemctl enable vncserver@:5.service
systemctl status vncserver@:5.service
Check that vncserver is listening to the ports other that localhost:
lsof -i -P | grep -i "listen" | grep Xvnc

If some kind of problem appears do below:
Remove not needed VNC display sessions (if any):
rm -f /tmp/.X11-unix/*
rm -f /tmp/.X*-lock
systemctl restart vncserver@:5.service
ps aux | grep vnc

On client machine

yum install tigervnc
Open TigerVNC Viewer (Applications > Internet) or vncviewer (from bash)
Specify serverNameOrIp:5095
Specify VNC password
then login using needed login and password (admin in our case)
Use F8 to make full-screen and to change other settings.



Tuesday, May 7, 2019

Copying multiple files with scp

You can copy all files in directory but problem is when you are trying to copy several files from huge directory. Below is the method I use to achieve needed:
  1. save list of names of needed files with their paths into the test file (ex: scpList.txt) - full path is needed!
  2. assign this list to the variable: scpList=$(echo \";for file in $(cat scpLoad.txt); do printf $file ; printf " "; done; echo \")
  3. manually copy variable content: echo $scpList
  4. mkdir ~/Desktop/scpFiles
  5. scp root@srvIP:insertCopiedVariableContentHere ~/Desktop/scpFiles 

Thursday, December 20, 2018

Installing Asterisk on CentOS7 (step-by-step)

Shrink home volume size and extend root volume size (if needed): 
umount /home
df -h # verify that /home is unmounted
parted -l # to find type of the home LV filesystem (fxs in my case)
lvremove /dev/centos/home # remove LV from the VG
lvcreate -L 5G -n home centos
mkfs.xfs /dev/mapper/centos-home
mount -a
lvs
lsblk
df -h
lvextend -l +100%FREE /dev/centos/root
lvextend -l +100%FREE /dev/centos/root 
xfs_growfs /dev/centos/root
df -h
lsblk
lvs

Setup networking:
systemctl stop NetworkManager
systemctl disable NetworkManager
chkconfig network on
systemctl start network
vi /etc/sysconfig/network-scripts/ifcfg-eth0 # assign IPADDR / PREFIX / GATEWAY / DNS1 / DNS2
vi /etc/sysconfig/network and add below two lines:
NETWORKING=yes
NOZEROCONF=true
systemctl restart network

Disable SELinux:
vi /etc/sysconfig/selinuix and change:
SELINUX=enabled to SELINUX=disabled

Setup NTP:
yum install -y ntp && ntpdate pool.ntp.org && chkconfig ntpd && service ntpd start
systemctl enable ntpd.service
systemctl status ntpd.service
systemctl start ntpd.service
systemctl status ntpd.service

Preinstall steps:
adduser asteriskpbx && passwd asteriskpbx && yum install sudo && visudo # uncomment #%wheel  ALL=(ALL)       ALL line 
vi /etc/group # add asteriskpbx to wheel group: wheel:x:10:root,asteriskpbx
usermod -L asteriskpbx #make asteriskpbx user nologin

Before Asterisk installation, install all prerequisite packages:
yum -y install make gcc gcc-c++ make subversion libxml2-devel ncurses-devel openssl-devel vim-enhanced man glibc-devel autoconf libnewt kernel-devel kernel-headers linux-headers openssl-devel zlib-devel libsrtp libsrtp-devel uuid libuuid-devel mariadb-server jansson-devel libsqlite3x libsqlite3x-devel epel-release.noarch bash-completion bash-completion-extras unixODBC unixODBC-devel libtool-ltdl libtool-ltdl-devel mysql-connector-odbc mlocate libiodbc

Initial setup MariaDB:
systemctl status mariadb
systemctl enable mariadb
systemctl start mariadb
systemctl status mariadb
/usr/bin/mysql_secure_installation

Asterisk Installation:
yum update -y
yum install wget -y
mkdir -p ~/src/asterisk/
cd src/asterisk/
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-13-current.tar.gz
tar -xzvf asterisk-13-current.tar.gz 
cd asterisk-13.23.1/
If your system is 64 bit and you want PJSIP:
./configure --libdir=/usr/lib64 --with-pjproject-bundled
Otherwise:
./configure
menuselect/menuselect --list-options
make
make install
make samples
make config
safe_asterisk
systemctl status asterisk

After install steps:
chown -R asteriskpbx:asteriskpbx /usr/lib/asterisk/
chown -R asteriskpbx:asteriskpbx /usr/lib64/asterisk/
chown -R asteriskpbx:asteriskpbx /var/lib/asterisk
chown -R asteriskpbx:asteriskpbx /var/spool/asterisk/
chown -R asteriskpbx:asteriskpbx /var/log/asterisk/
chown -R asteriskpbx:asteriskpbx /var/run/asterisk/
chown -R asteriskpbx:asteriskpbx /usr/sbin/ast
chown -R asteriskpbx:asteriskpbx /usr/sbin/asterisk
chown -R asteriskpbx:asteriskpbx /var/log/asterisk
asterisk -r
exit

If want -  load modules needed for PJSIP

vi /etc/asterisk/modules.conf
[modules]
;AUTOLOAD
autoload=yes
;LOAD
preload => res_odbc.so
preload => res_config_odbc.so
load => res_pjsip.so
load => res_pjsip_pubsub.so
load => res_pjsip_session.so
load => chan_pjsip.so
load => res_pjsip_exten_state.so
load => res_pjsip_authenticator_digest.so
load => res_timing_timerfd.so

vi /etc/asterisk/asterisk.conf and uncomment and change this 2 lines:
runuser = asteriskpbx           ; The user to run as.
rungroup = asteriskpbx        ; The group to run as.

asterisk -rx "core restart now"

setup CEL with ODBC

odbcinst -j # verify that odbcinst.ini & odbc.ini are created
mysql -u root -p mysql
> CREATE USER 'asterisk'@'%' IDENTIFIED BY 'some_secret_password';
> CREATE DATABASE asterisk;
> GRANT ALL PRIVILEGES ON asterisk.* TO 'asterisk'@'%';

mysql -u asterisk -p asterisk # check access with asterisk user
odbcinst -q -d # verify that [MySQL] driver is seen
vi /etc/odbc.ini and add following:
[asterisk-connector]  # connector-name / DSN
Description =MySQL connection to 'asterisk' database
Driver =MySQL # driver name from the odbcinst.ini
Database =asterisk # database to connect
Server =localhost  # we'll connect to the server itself
charset =UTF8
UserName =asterisk # DB user-name for asterisk DB
Password =some_secret_password # asterisk DB-user password
Port =3306
Socket =/var/lib/mysql/mysql.sock

echo "select 1" | isql -v asterisk-connector asterisk 'some_secret_password' # check connection

Add connector to the asterisk res_odbc.conf:
[asterisk]
enabled => yes
dsn => asterisk-connector ; DSN is Data Source Name (from odbc.ini)
username => asterisk
password => some_secret_password
;pooling => no ; old option, replaced by max_connections
;limit => 1  ; old option, replaced by max_connections
pre-connect => yes
max_connections => 1

asterisk -rx "core restart now"
asterisk -rx "odbc show" # must show "Number of active connections: 1 (out of 1)"

vi cel.conf add following:
[general]
enable=yes
apps=all
events=all

vi cel_odbc.conf add following:
[mycel]
connection=asterisk
table=cel

vi /etc/my.cnf  add following under [mysqld]:
character_set_server=utf8

mysql -u asterisk -p asterisk
CREATE TABLE cel
(
id INT(20) NOT NULL AUTO_INCREMENT,
eventtype INT(11) COLLATE utf8_general_ci NOT NULL,
eventtime TIMESTAMP NOT NULL,
userdeftype VARCHAR(30) COLLATE utf8_general_ci NULL,
cid_name VARCHAR(80) COLLATE utf8_general_ci NULL,
cid_num VARCHAR(80) COLLATE utf8_general_ci NULL,
cid_ani VARCHAR(80) COLLATE utf8_general_ci NULL,
cid_rdnis VARCHAR(80) COLLATE utf8_general_ci NULL,
cid_dnid VARCHAR(80) COLLATE utf8_general_ci NULL,
exten VARCHAR(30) COLLATE utf8_general_ci NULL,
context VARCHAR(30) COLLATE utf8_general_ci NULL,
channame VARCHAR(30) COLLATE utf8_general_ci NULL,
appname VARCHAR(30) COLLATE utf8_general_ci NULL,
appdata VARCHAR(150) COLLATE utf8_general_ci NULL,
accountcode VARCHAR(30) COLLATE utf8_general_ci NULL,
peeraccount VARCHAR(30) COLLATE utf8_general_ci NULL,
uniqueid VARCHAR(30) COLLATE utf8_general_ci NULL,
linkedid VARCHAR(30) COLLATE utf8_general_ci NULL,
amaflags INT(11) NULL,
userfield VARCHAR(30) COLLATE utf8_general_ci NULL,
peer VARCHAR(30) COLLATE utf8_general_ci NULL,
PRIMARY KEY (id),
KEY uniqueid (uniqueid)
)
ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

asterisk -r asterisk
*CLI> module reload res_odbc.so asterisk
*CLI> module reload cel_odbc.so asterisk
*CLI> cel show status