Saving sip registration/unregistration in Asterisk into MySQL - mysql

How can I save sip accounts registration/unregistration into MySQL?
I have only one idea at the moment ...
Now I'm trying to parse asterisk's full log:
cat /var/log/asterisk/full | grep "egistered SIP " | awk '{print $1,$2,$3,$6,$7,$8}' | sort -t" " -k2 -n
and saving information into table:
sip_registrations (id, sip, datetime, event[1 - registration; 2 - unregistration] )
Is there any easier way?
Thanks.

You can use sip realtime(peers from db) and set it to update state of peer.
You can listen AMI interface for event.
You can read logs and parse.

Related

Instruct mysql to output CSV with read permissions? (osx)

Is there a simple way to output a CSV from MySQL that can be read by all users?
The issue is as follows. On macosx by default the user is _msyql who is a member of the wheel group. Also by default the INTO command will create a file with read permissions for the wheel group only. Since adding a user to the wheel group is not good, this would seem to imply there is no good simple way to make use of INTO.
It really depends on your definition of "simple".
Instead of using the select into clause (which causes the behavior you observed), you can direct mysql client output to stdout and do the csv creation yourself:
mysql mydatabase -B -e 'select * from mytable' | sed 's/\t/,/g' > output.csv
There's more info covering sed processing corner cases here.

How do you send multiple commands, including an SQL query, via an SSH-Tunnel with Plink? [duplicate]

This question already has answers here:
Escaping parentheses within parentheses for batch file
(4 answers)
Closed 2 years ago.
I am trying to create a Batch file from a Python script which executes Plink to send an SQL-Query to an external Database via SSH. The script would have to activate a batch file with multiple command lines to be sent to the server.
Researching on the internet I have found, that a solution akin to the code snipped below should work.
(
echo command 1
echo command 2
...
) | plink.exe user#hostname -i sshkey.ppk
Entering my commands would yield the following:
(
echo mysql -u admin -pPassword Database
echo INSERT INTO Table VALUES(DEFAULT, (SELECT ID FROM Another_Table WHERE Another_ID = 'foo'), 'bar', 'foobar', 0, 'date', 1);
) | plink.exe user#hostname -i sshkey.ppk
The problem I have is that I am getting the following error: 'bar' can't be processed syntactically at this point. (I am sorry if the translation might be off here, english is not my first language).
I have checked if some special characters have to be escaped, but have not found any conclusive answers. Note, that the first command is correct and works as intended on its own; only the second command seems to be faulty. Would anybody be willing to provide me a solution?
So the answer here is that you need to escape the closing parenthesis TWICE, not only once, and thus have to use three "^" characters. This is because the command inside the brackets is parsed twice and the second "^" needs to be escaped for the first parsing, thus requiring a third character.
See here for details: Escaping parentheses within parentheses for batch file
The code would therefore look like this:
(
echo mysql -u admin -pPassword Database
echo INSERT INTO Table VALUES(DEFAULT, (SELECT ID FROM Another_Table WHERE Another_ID = 'foo'^^^), 'bar', 'foobar', 0, 'date', 1^^^);
) | plink.exe user#hostname -i sshkey.ppk

update Postgres psql prompt if semicolon is forgotten

tldr: Can psql be altered to mimic the MySQL CLI w/ respect to improperly terminated statements?
In the MySQL CLI, when the user fails to properly terminate a statement, the prompt changes to indicate a semicolon was forgotten (indentation, adds ->):
MariaDB[(none)]> describe testing123
->
The Postgres psql CLI prompt doesn't change if a semicolon is forgotten:
zach=# \dt
List of relations
Schema | Name | Type | Owner
--------+------------+-------+-------
public | testing123 | table | zach
(1 row)
zach=# drop table testing123
zach-# ;
DROP TABLE
zach=#
Instead, you just don't see the expected confirmation message (in this case, DROP TABLE). So it's not nothing, but sorta a catch-22 if you don't know what message you should be looking for or if there is even a confirmation message at all for the command you're running.
\set PROMPT2 '-%# '
issue that at the start of your session, or you could add that to your .psqlrc file
I settled on updating .psqlrc to mimic the MySQL CLI by adding: \set PROMPT2 ' -> '
If you want a clue from psql about where your statement went wrong, another option is \set PROMPT2 '[more] %R > ' which tells you:
whether psql expects more input because the command wasn’t terminated yet, because you are inside a /* ... */ comment, or because you are inside a quoted or dollar-escaped string [ThoughtBot blog]

Can MySQL check that file exists?

I have a table that holds relative paths to real files on HDD. for example:
SELECT * FROM images -->
id | path
1 | /files/1.jpg
2 | /files/2.jpg
Can I create a query to select all records pointing to non-existent files? I need to check it by MySql server exactly, without using an iteration in PHP-client.
I would go with a query like this:
SELECT id, path, ISNULL(LOAD_FILE(path)) as not_exists
FROM images
HAVING not_exists = 1
The function LOAD_FILE tries to load the file as a string, and returns NULL when it fails.
Please notice that a failure in this case might be due to the fact that mysql simply cannot read that specific location, even if the file actually exists.
EDIT:
As #ostrokach pointed out in comments, this isn't standard SQL, even though MySQL allows it, to follow the standard it could be:
SELECT *
FROM images
WHERE LOAD_FILE(PATH) IS NULL
The MySQL LOAD_FILE command has very stringent requirements on the files that it can open. From the MySQL docs:
[LOAD_FILE] Reads the file and returns the file contents as a string. To use this function, the file must be located on the server host, you must specify the full path name to the file, and you must have the FILE privilege. The file must be readable by all and its size less than max_allowed_packet bytes. If the secure_file_priv system variable is set to a non-empty directory name, the file to be loaded must be located in that directory.
So if the file can't be reached by the mysql user or any of the other requirements are not satisfied, LOAD_FILE will return Null.
You can get a list of IDs that correspond to missing files using awk:
mysql db_name --batch -s -e "SELECT id, path FROM images" \
| awk '{if(system("[ -e " $2 " ]") == 1) {print $1}}' \
>> missing_ids.txt
or simply using bash:
mysql db_name --batch -s -e "SELECT id, path FROM images" \
| while read id path ; if [[ -e "$path" ]] ; then echo $id ; done
>> missing_ids.txt
This also has the advantage of being much faster than LOAD_FILE.
MYSQL only handles the Database so there is no way for you to fire an SQL Statement to check on the HDD if the file exists. You need to iterate over the rows and check it with PHP.
It's not possible using stock MySQL. However you can write UDF (user-defined function), probably in C, load it using CREATE FUNCTION statement and use it from MySQL as you would use any built-in function.

What is the easiest way to extract sprint start and end dates from the JIRA db?

I'm trying to extract the start and end days of my sprints from the Jira database. This would seem like a simple task, but it really (as far as I have found out at least) is not.
While trying to figure this out I found one solution, but it seems to me so cumbersome and difficult that I have problems thinking this is the only way.
Here is what I have found out:
Sprints are not a native Jira expression and the Greenhopper plugin uses the projectversion table to represent sprints.
The projectversion table contains some information about the sprint, like name, what project it belongs to and the release date. The release date can be thought of as a sprint end date, but the start date is missing.
If you run back to back sprints maybe the start date of a sprint can be set to the release date of the previous sprint plus one day? But that is really not a good solution.
So I searched through the Jira data model and the best and only reference I found to a sprint start date was in the property structure.
You can define properties and assign them values. In the main table of this structure, the propertyentry table, I found entries like this:
ID ENTITY_NAME ENTITY_ID PROPERTY_KEY propertytype
----- -------------- ------------ ------------------------------------ ------------
10288 GreenHopper 10010 CONFIGURATION 6
10304 GreenHopper 10012 CONFIGURATION 6
10312 GreenHopper 10013 CONFIGURATION 6
10449 GreenHopper 10014 CONFIGURATION 6
So GreenHopper have added a property with the key set to CONFIGURATION. The etity_id field references project.id and the configuration property is a project configuration. The property_type is set to 6 which tells us to look for the value in the propertytext table.
The value stored in the propertytext table reveals it self as a XML string containing different information about the project, among it entries like this:
<entry>
<string>BURNDOWN_START_DATE_10074</string>
<long>1316988000000</long>
</entry>
There it is. The best equivalent I have found to a sprint start date. Hidden in an xml string in a property table.
My question is: Is this really the only way to find my sprint starting dates?
There seems to be no way of getting end and start date of sprint via Jira SOAP/REST API.
You can extract the start and end dates of your sprints using:
com.pyxis.greenhopper.jira.configurations.ProjectConfiguration#getVersionStartDate
com.pyxis.greenhopper.jira.configurations.ProjectConfiguration#getVersionEndDate
To use this class you can write a Jira plugin - Developing with the Atlassian Plugin SDK
Another option is to write GreenHopper module - GreenHopper Developer Documentation
I don't recommend accessing the JIRA database directly if it can be avoided. The undocumented JIRA Agile REST API such as rest/greenhopper/1.0/rapid/charts/sprintreport.json?rapidViewId=652&sprintId=577 where rapidViewId is the board id,
gives you the Sprint information. This and other REST resources can be seen in the jira-python library at http://jira-python.readthedocs.org/en/latest/
The easiest way to find start date and end date of sprint in Agile jira is to hit jiraschema.AO_60DB71_SPRINT table. This stores start_date, end_date as big int. Jira for some reasons stores these date formats as int data type. To convert the int to date data type, here is the query in MS Sql. You can easily change it to some other database if required.
###This query pulls start_date, end_date, completed_date of all non active sprints in MS SQL.###
select ID, Name SprintName
,START_DATE / 60 / 60 / 24 / 1000 + CAST('12/31/1969' as datetime)+1 StartDate
,END_DATE / 60 / 60 / 24 / 1000 + CAST('12/31/1969' as datetime)+1 EndDate
,COMPLETE_DATE / 60 / 60 / 24 / 1000 + CAST('12/31/1969' as datetime)+1 CompletedDate
FROM
AO_60DB71_SPRINT as sprint
where COMPLETE_DATE is not null
SELECT * FROM a0_60db71_sprint
This is a MySQL question, not java.
Connect to your JIRA MySQL Database and look for a table that matches *_sprint
The fields on the above table are:
Closed (boolean)
ID (key)
Start_Date (timestamp)
End_Date (timestamp)
Complete_Date (timestamp).
I was given a task recently to get list of sprints with dates for specific project. First I needed to find project ID from Project table and customfield ID for field Sprint from tables customfield/customfieldvalue.
Here is the result
select
p.pname as "Project Name",
s.NAME as "Sprint Name",
from_unixtime(s.START_DATE / 1000) as "Start Date",
from_unixtime(s.END_DATE / 1000) as "End Date",
from_unixtime(s.COMPLETE_DATE / 1000 ) as "Complete Date"
from
customfieldvalue as c,
jiraissue as i,
project as p,
AO_60DB71_SPRINT as s
where
p.id = <project ID> and p.id = i.project and
c.issue = i.id and c.customfield = <customfield ID> and
c.stringvalue = s.id
group by s.name
;
Our mysql server is in different time zone so I had to modify output time.
...
from_unixtime(s.START_DATE / 1000) - interval 1 hour as "Start Date",
...
Maybe it will help somebody
Not sure! why JIRA doesn't provide a very simple Rest Endpoint to just spit all sprints info. Why I have to deal with board/boardID to find sprints in that board, why I have to iterate over all sprints.
I'm administrator user and still hitting some of the sprint # gives me, Sprint does not exist.
Anyways, here's a work-around script.
#!/bin/bash
JIRA_URL="http://my_jira_server:8080"
users_sprint_limit_cmd_line_arg="$1"
# First parameter passed to the script is a NUMBER (for how many sprints a user wants to iterate over.
## I know!! it's a work-around for dealing with "Sprint does not exist" and
## becasue there's no shitty direct JIRA Rest API that exist, to query JIRA server, to spit all SPRINTS with info (start/end date) in just one call.
## You can use API token (or base64 hash). I'm just going rouge here.
user="a_user_user_who_can_read_any_sprint_or_serviceuser_or_admin"
pass="D00M4u!"
## Set build number variable
b_no=${BUILD_NUMBER:="999999"}
## At the end, you'll have a Temp file will store all sprints info, Valid will contain only valid sprints.
temp_sprint_file="/tmp/all_sprints_startdates_${b_no}_temp.txt"
valid_sprint_file="/tmp/all_sprints_startdates_${b_no}.txt"
## Clean files
rm ${temp_sprint_file} ${valid_sprint_file} || true;
## Sprint counter
sprint_no=1
result="ToBeSet"
## Iterate over all sprints and find their start/stop dates.
## -- This is one-odd way to find sprint's start/end dates, but it works!!
## -- A user can pass a larger value in while condition "-lt value" via cmd line 1st param.
while [[ $sprint_no -lt ${users_sprint_limit_cmd_line_arg} ]];
do
## assumes 'jq' is installed. --OR run: sudo yum install jq
## --------------------------
result="$(curl -s -u $user:$pass -X GET -H 'Content-Type: application/json' "${JIRA_URL}/rest/agile/1.0/sprint/${sprint_no}" | \
jq | \
egrep "name|startDate|endDate" | \
cut -d'"' -f4 | \
sed "s/T[0-9][0-9]:[0-9][0-9].*$//" | \
tr '\012' ',' | \
sed "s/,$//")";
echo "${result}" >> ${temp_sprint_file}
((sprint_no++));
done
## Find valid sprints which have valid start/end dates.
grep "[A-Za-z],[0-9].*,[0-9]" ${temp_sprint_file} > ${valid_sprint_file};
echo -e "\n\n-- Sprints and Start/End Date file is available here: ${valid_sprint_file}\n\n"
Running cat command on this generated sprint data file will give you, something like:
1 Trumpy Trump,2019-01-09,2019-01-23
2 Magical Modi,2019-01-18,2019-02-01
Where, you can add a line in the above script, to use it as a pure CSV file by having a header line i.e. Sprint_Name,Sprint_Start_Date,Sprint_End_Date, I just didn't do that as my use case was to use just this file as a reference file.
A related post regarding dates: BASH: How to find no. of days (considering only "Network / Business Days") between two dates (i.e. exclude weekends Saturday/Sunday)