Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Transactions in Redis part3

In order to implement transaction in Python you need to use pipline and there is no such a thing as exec, multi, etc.

r = redis.Redis()
p = r.pipeline()
p.set("transError", var)
p.execute()

MULTI, SET, EXEC sent when p.execute() is called. To omit the MULTI/EXEC pair, use r.pipeline(transaction=False).

More info: https://redis-py.readthedocs.io/en/latest/#redis.Redis.pipeline

#python #redis #transaction #multi #exec
In Pycharm I wrote something like below in multiple lines in the file:


method_name='get_account'


I wanted to add _v2 to all the method names, what I did was to use regex in PyCharm replace functionality. Press Command+R in order to open replace dialog. In the dialog there is an option called Regex, tick the checkbox in front of it and in find section write:


method_name='(.*)'


It will find all lines which has different names: .* and put that in a variable. (you can put something you have found in a variable by using parenthesis).

Now we can access the variable using $1. We now need to put the below code in replace section:


method_name='$1_v2'


The above code will put method name using $1 and the append _v2 to all the methods.


#pycharm #regex #find #replace
Tech C**P
What is Capped Collections in MongoDB? Capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. Capped collections work in a way similar to circular buffers: once a…
Earlier we explained about capped collection in MongoDB. Today we just want to add something more to it.


Query a Capped Collection:
If you perform a find() on a capped collection with no ordering specified, MongoDB guarantees that the ordering of results is the same as the insertion order.

To retrieve documents in reverse insertion order, issue find() along with the sort() method with the $natural parameter set to -1, as shown in the following example:

db.cappedCollection.find().sort( { $natural: -1 } )


#mongodb #mongo #capped_collection #natural #natural_order
If you go to your mongodb data directory where all database data will be stored you will see:

$ ls -lh YOUR_DATABASE.*
-rw------- 1 mongodb mongodb 64M Feb 6 07:26 YOUR_DATABASE.0
-rw------- 1 mongodb mongodb 512M Feb 6 07:26 YOUR_DATABASE.1
-rw------- 1 mongodb mongodb 16M Feb 6 07:26 YOUR_DATABASE.ns

If you have given the size of 524288000 in collection creation, then you would see 512MB for your DB size. You can also see the whole size inside of mongo shell.

rs0:PRIMARY> show dbs
local 0.203GB
YOUR_DATABASE 0.578GB

#mongodb #mongo #capped_collection
If you are a python programmer tell me which return is actually returned? :D

def test():
try:
return 'Inside try block'
except:
return 'Inside Exception'
finally:
return 'Finally block'

print test()

#python #try #except #finally
How to monitor network cards on Icinga2? (part-1)

Yesterday I've been on a task of monitoring network cards of all our servers and infrastructure to check the bandwidth in/out and send alarms based on some criteria. In Icigna2 we have a library from nagios called check_nwc_health. Download the script from https:// labs.consol.de/nagios/check_nwc_health/index.html.

Move the script to /usr/lib/nagios/plugins on a server that you have installed Icinga2. If you run it all alone you will get some helps that you could be useful.

Some important usages of the script:

- list interfaces of a specific server (we assume snmp has been installed on the destination server):

./check_nwc_health --mode list-interfaces --hostname YOUR_TARGET_SERVER_IP  --community YOUR_COMMUNITY_STRING

The output would be something like below (it can be different in your case):

000001 lo
000002 Device 1af4:0001 2
000003 Device 1af4:0001 3
000004 docker0
OK - have fun


The interface name is given in front of serial numbers which is lo, Device 1af4:0001 2 or docker0. These interface names are important and will be used in icinga2 to add network card to hosts.


Another mode for the script is interface-usage that shows in/out bandwidth. The output can be something like follow:

OK - interface Device 1af4:0001 2 (alias eth0) usage is in:0.00% (7058.67bit/s) out:0.00% (5603.67bit/s) | 'Device 1af4:0001 2_usage_in'=0%;80;90;0;100 'Device 1af4:0001 2_usage_out'=0%;80;90;0;100 'Device 1af4:0001 2_traffic_in'=7058.67;0;0;0;0 'Device 1af4:0001         2_traffic_out'=5603.67;0;0;0;0

OK, the important part is over and we can list all server network interfaces plus the usage of a specific network interface. In the next part we will explain the Icinga2 part to add the command and the service to icinga2.


#icinga2 #icinga #nagios #check_nwc_health #network #monitor
How to monitor network cards on Icinga2? (part-2)

Ok for now we have added the plugin to nagios folder and ran some tests on target server's network interfaces. We need to add a command to Icinga2 to use it in service section of Icinga2. To create a new command create a new file in /etc/icinga2/conf.d/commands/check_nwc_command.conf and with the following content:

object CheckCommand "YOUR_COMMAND_NAME" {
import "plugin-check-command"
command = [ PluginDir + "/check_nwc_health", "--mode", "interface-usage" ]
arguments = {
"-H" = "$address$"
"-C" = "$community$"
"--name" = "$int$"
}
}

In brief it creates a new command called YOUR_COMMAND_NAME that calls the script check_nwc_health with interface-usage argument to get the bandwidth data.


Now we need to use this command in a service. We have to create a new service which will be used in our hosts configuration sections /etc/icinga2/conf.d/services/if_traffic.conf:

apply Service for (display_name => config in host.vars.int) {
import "generic-service"
check_command = "YOUR_COMMAND_NAME"
vars += config
assign where host.vars.int
}

Again in brief the service will be applied on hosts that have a variable section of int in their configuration that we will see a little bit later. YOUR_COMMAND_NAME is the name that we have given in the first part when creating the command.


The final part is to add this service to your desired host. Go to /etc/icinga2/conf.d/hosts and open the file which relates to your host. Host files content start with:

object Host "host-54 (Infra)" {


Add the service like below into your host:

vars.int["YOUR DISPLAY NAME"] = {
int = "Device 1af4:0001 2"
community = "YOUR SERVER COMMUNITY STRING"
}

int is the part that we give the interface name, this should be given from the output of list-interfaces in part-1.

You can go even further like me :) and add these data into Grafana dashboard to have a better understanding of what is happening around you.

#icinga2 #icinga #service #host #command #nagios #interface #network
I can talk about Icinga2 for about a book! From memory, CPU, disk usage monitoring to even creating custom plugins with python or other languages to monitor your specific use case (Let's you want to monitor your microservice modules).

Maybe in the future I do so! :)
One of the greatest examples of Geospatial queries is MongoDB's tutorial about finding the nearest restaurant. This example can be applied to online taxis like Snapp, tap30 or some social networks. Read the below article if you are crazy as hell to learn new things like me :)


https://docs.mongodb.com/manual/tutorial/geospatial-tutorial/

#mongodb #mongo #geospatial #geoWithin #geoNear #geo #2dsphere #geoIntersects
در طول دوره کاری خود بارها با بحث تغییر نیازمندیهای بیزینس مواجه شده ام و در تمامی موارد به جرات می توانم بگویم که هر زمانی که تغییر نیازمندی باعث تغییر زیاد در ماژولها میشود و من از انجام تغییر از راه میانبر و به قولی کثیف پیش برده ام یکجا همون قضیه یقه من راه گرفته (bite in the ass) و هر بار که تغییر مورد نظر با اینکه زیاد هم بوده انجامش داده ام توانستم که به بهترین نحو مسئله را حل کنم و در آینده هم خودم از کارم راضی بودم.

نکته مهم قضیه این هست که همیشه مدیران عامل به سریعترین وجه ممکن خروجی را از شما میخواهند، در این گونه مواقع سریع تسلیم نشوید و کار کثیف و میانبر که بد هست را انجام ندهید. مدیر عامل فردی ست مثل شما با آنها صحبت کنید و بگویید به این دلایل زمان بیشتری میخواهید و باید به این صورت انجام شود وگرنه در آینده به مشکل میخوریم، در اکثر موارد موفق می شوید که زمان بیشتری بخرید و کار را بصورت درست انجام دهید. در کنار زمان، این مسئله به اخلاق برنامه نویسی نیز بازمیگردد و برخی افراد حوصله کار زیاد والبته درست را ندارند!!! عجیب است ولی خود من هم تا چند سال پیش در همین دسته جای میگرفتم.

نکته اینجاست که اخلاقتون رو خوب کنید :)
و نکته بعد این هست که با مدیران خود صحبت کنید و آنها را در قبال کاری که میکنید توجیح کنید.

#دلنوشته
Upgrade mongoDB from 3.4 to 3.6:

Here we persume you are on debian 8 jessie.

1- import public key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv                        2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5


2- create apt sources file:

echo "deb https://repo.mongodb.org/apt/debian jessie/mongodb-org/3.6 main" | sudo tee /   etc/apt/sources.list.d/mongodb-org-3.6.list

3- update repo

sudo apt-get update


4- install the MongoDB packages

sudo apt-get install -y mongodb-org=3.6.2 mongodb-org-server=3.6.2 mongodb-org-shell=3.  6.2 mongodb-org-mongos=3.6.2 mongodb-org-tools=3.6.2

* it will ask for config overwrite, if you want to take backup take a backup from config and then overwrite it.


#mongodb #mongo #mongodb36 #database #upgrade #mongodb34
Check grants of a specific user on MySQL:

SELECT sql_grants FROM common_schema.sql_show_grants WHERE user='app';

Please make sure that you have permissions on getting grants list, otherwise permission denied will be returned back.

#mysql #grants #sql_grants #database